就是考stringbuilder的用法啊。。public static void main(String[] args) { String word = "two", target = "Two"; String str = "Nature has given us that two ears, two eyes, and but..."; StringBuilder stringBuilder = new StringBuilder(str.length()); stringBuilder.append(str); int index = str.lastIndexOf(word); stringBuilder = stringBuilder.replace(index, index + word.length(), target); System.out.println(stringBuilder.toString()); }
System.out.println("Nature has given us that two ears, Two eyes, and but one tongue, to the end that we should hear and see more than we speak.");
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak"; int index = str.lastIndexOf(" two "); String str1 = str.substring(0, index + 1); String str2 = str.substring(index + 1, index + 2); String str3 = str.substring(index + 2);
优化一下 String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak"; StringBuffer sb = new StringBuffer(str); int index = str.lastIndexOf(" two "); char c = str.charAt(index + 1); String ss = String.valueOf(c).toUpperCase(); sb = sb.replace(index + 1, index + 2, ss); System.out.println(sb.toString());
第四题//生成指定长度的随机字符串 public static String createRandomString(int length) { char ch[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1' }; Random random = new Random(); if (length > 0) { int index = 0; char[] temp = new char[length]; int num = random.nextInt(); for (int i = 0; i < length % 5; i++) { temp[index++] = ch[num & 63]; num >>= 6; } for (int i = 0; i < length / 5; i++) { num = random.nextInt(); for (int j = 0; j < 5; j++) { temp[index++] = ch[num & 63]; num >>= 6; } } return new String(temp, 0, length); } else if (length == 0) { return ""; } else { throw new IllegalArgumentException(); } } public static void main(String[] args) { String tenLength = ""; int length = 10000; String strTime = ""; long s = System.currentTimeMillis(); for (int i = 0;i < length;i++){ tenLength = createRandomString(10); strTime += tenLength; } System.out.println("String use : " + (System.currentTimeMillis() - s) +"ms." + strTime); StringBuffer sbTime = new StringBuffer(); s = System.currentTimeMillis(); for (int i = 0;i < length;i++){ tenLength = createRandomString(10); sbTime.append(tenLength); } System.out.println("StringBuffer use : " + (System.currentTimeMillis() - s) +"ms." + sbTime); }
StringBuffer 用时少
七楼第四题全解,五楼第五题提示,去看看String类和StringBuffer的常用方法就知道了
public class Test{ public static void main(String[] args){ String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak"; System.out.println(str); System.out.println(str.replaceAll("(^.*)(?<=\\b)two(?=\\b)","$1Two")); } }
indexOf获取最后一个two的位置,将获得的索引位置的字母变大写toUpperCase
String name="sadasdastwosadasdasdastwoasdasdasdtwopp"; StringBuffer sb = new StringBuffer(name); int s =name.lastIndexOf("two"); sb.replace(s, s+1, name.substring(s, s+1).toUpperCase());
String str = "Nature has given us that two ears, two eyes, and but"; int index = str.lastIndexOf("two"); String t = str.substring(index,index+1); System.out.println(t.toUpperCase());
#第4题:String与StringBuffer的性能区别? 答:使用String每次相加都会产生临时字符串,如果拼接10000个临时字符串,会产生将近2万个临时字符串,非常耗内存.而StringBuffer则 没有这个问题,StringBuffer是线程安全的,适合多线程.如果是单线程环境建议使用StringBuilder//#第5题: public static void main(String[] args) { String str="Nature has given us that two ears,two eyes,and but one tongue," + "to the end that we should hear and see more than we speak";
int index=str.lastIndexOf("two"); char ch=str.charAt(index);
String word = "two", target = "Two";
String str = "Nature has given us that two ears, two eyes, and but..."; StringBuilder stringBuilder = new StringBuilder(str.length());
stringBuilder.append(str); int index = str.lastIndexOf(word);
stringBuilder = stringBuilder.replace(index, index + word.length(), target);
System.out.println(stringBuilder.toString());
}
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
int index = str.lastIndexOf(" two ");
String str1 = str.substring(0, index + 1);
String str2 = str.substring(index + 1, index + 2);
String str3 = str.substring(index + 2);
String newStr = str1.concat(str2.toUpperCase()).concat(str3);
System.out.println(newStr);
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
很清楚了吧
利用lastindexof
StringBuffer sb = new StringBuffer(str);
int index = str.lastIndexOf(" two ");
char c = str.charAt(index + 1);
String ss = String.valueOf(c).toUpperCase();
sb = sb.replace(index + 1, index + 2, ss);
System.out.println(sb.toString());
public static String createRandomString(int length) {
char ch[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1' };
Random random = new Random();
if (length > 0) {
int index = 0;
char[] temp = new char[length];
int num = random.nextInt();
for (int i = 0; i < length % 5; i++) {
temp[index++] = ch[num & 63];
num >>= 6;
}
for (int i = 0; i < length / 5; i++) {
num = random.nextInt();
for (int j = 0; j < 5; j++) {
temp[index++] = ch[num & 63];
num >>= 6;
}
}
return new String(temp, 0, length);
}
else if (length == 0) {
return "";
}
else {
throw new IllegalArgumentException();
}
} public static void main(String[] args) {
String tenLength = "";
int length = 10000;
String strTime = "";
long s = System.currentTimeMillis();
for (int i = 0;i < length;i++){
tenLength = createRandomString(10);
strTime += tenLength;
}
System.out.println("String use : " + (System.currentTimeMillis() - s) +"ms." + strTime); StringBuffer sbTime = new StringBuffer();
s = System.currentTimeMillis();
for (int i = 0;i < length;i++){
tenLength = createRandomString(10);
sbTime.append(tenLength);
}
System.out.println("StringBuffer use : " + (System.currentTimeMillis() - s) +"ms." + sbTime);
}
public class Test{
public static void main(String[] args){
String str = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
System.out.println(str);
System.out.println(str.replaceAll("(^.*)(?<=\\b)two(?=\\b)","$1Two"));
}
}
StringBuffer sb = new StringBuffer(name);
int s =name.lastIndexOf("two");
sb.replace(s, s+1, name.substring(s, s+1).toUpperCase());
int index = str.lastIndexOf("two");
String t = str.substring(index,index+1);
System.out.println(t.toUpperCase());
答:使用String每次相加都会产生临时字符串,如果拼接10000个临时字符串,会产生将近2万个临时字符串,非常耗内存.而StringBuffer则
没有这个问题,StringBuffer是线程安全的,适合多线程.如果是单线程环境建议使用StringBuilder//#第5题:
public static void main(String[] args)
{
String str="Nature has given us that two ears,two eyes,and but one tongue,"
+ "to the end that we should hear and see more than we speak";
int index=str.lastIndexOf("two");
char ch=str.charAt(index);
StringBuilder stringBuilder=new StringBuilder();
stringBuilder=stringBuilder.append(str.substring(0, index)).append(Character.toUpperCase(ch)).append(str.substring(index+1));
System.out.println(stringBuilder);
}