我希望更好地理解字符串。我正在编写一个需要大量字符串的程序。但是,很多字符串非常相似,只需在字符串结尾处更换不同的单词即可。
例如:
String one = "I went to the store and bought milk"
String two = "I went to the store and bought eggs"
String three = "I went to the store and bought cheese"
我的问题是,在处理字符串时,哪种方法最适合?将两个字符串连接起来是否比仅使用静态字符串更具有性能或内存管理方面的优势?
例如:
String one = "I went to the store and bought "
String two = "milk"
String three = "cheese"
String four = one + two
String five = one + three
我只是想找出处理所有这些字符串的最佳方法。(如果需要知道我使用的字符串数量,我目前有50个,但数量可能会超过很多)
static final
字符串,在这里很好地解释了。对于常量部分使用字符串是一个不错的主意。 - pietv8x