02字符串
介绍:字符串时使用双引号括起来的字符序列。
相关操作:
1.字符串的创建
String str = "hello";
String str = new String("hello");
char[] a = {'h','e','l','l','o'};
String str = new String(a);
StringBuffer str = new StringBuffer();
StringBuffer str = new StringBuffer(2);
StringBuffer str = new StringBuffer("hello");
2.常用方法
String str = "hello" + "world";
String str1 = "hello";
String str2 = "world";
String str = str1.concat(str2);
输出结果:str = helloworld
int len = str.length();
输出结果:5;
String str1 = "world";
boolean flag = str.equals(str1);
输出结果:flag = false;
String str1 = "Hello";
boolean flag = str.equalsIgnoreCase(str1);
输出结果:flag = true;
String str = "a";
String str1 = "b";
int x = str.compareTo(str1);
输出结果:x = -1;
String str1 = str.substring(1,4);
输出结果:str1 = "ell";
String str = "hello";
int index = str.indexOf("l",3);
输出结果:index = 3;
char i = str.charAt(0);
输出结果:i = 'h';
String oldstr = "llo";
String newstr = " ";
str = str.replace(oldstr,newstr);
输出结果:str = "he";
String newstr = " hello ";
newstr = newstr.trim();
输出结果:newstr = "hello";
String str = "Hello";
String upstr = str.toUpperCase();
String lowstr = str.toLowerCase();
输出结果:upstr = "HELLO" lowstr = "hello"
String str = "a,b,c,d,e";
String[] a = str.split(",");
输出结果:a = ["a","b","c","d","e"];
char[] arr = str.toCharArray();
输出结果:arr = ['h','e','l','l','o'];
strbuffer.reverse();
输出结果:strbuffer = "olleh";
strbuffer.deleteCharAt(2);
输出结果:strbuffer = "helo";
strbuffer.insert(0,"hello");
输出结果:strbuffer = "hellohello";
strbuffer.delete(0,1);
输出结果:strbuffer = "ello";
String s = "ok";
strbuffer.append(s);
输出结果:strbuffer = "hellook";