java中字符串的操作

时间:2020-12-25 04:22:23

//创建一个字符数组

char[] charArr = {'a','b','c','d','e','f','g'};

//创建一个字符串

String str = new String(charArr);

System.out.println(str);

String aStr = "abcdefg";

//字符串的比较

boolean results = str.equals(aStr);

System.out.println(results);

//或得所在索引的字符

char ch = str.charAt(3);

System.out.println(ch);

//或得字符的索引

int index = str.indexOf("a");

System.out.println(index);

//得到字符串的一部分字符串

char[] chArr = new char[10];

str.getChars(1,3,chArr,0);

System.out.println(chArr);

//截取字符串

String subStr = str.substring(2);

System.out.println(subStr);

//字符串的拼接

String string1 = "i am string1";

String string2 = "i am string2";

String superStr = string1.concat(string2);

System.out.println(superStr);

//实现字符串大小写的转换

String lowStr = "abcdefghijklmnopqrstuvwsyz";

String upStr = lowStr.toUpperCase();

System.out.println(upStr);

System.out.println(upStr.toLowerCase());

//去掉字符换开头和结尾的空格

String trimStr = " i am a string ";

System.out.println(trimStr.trim());

//将任意类型转化为字符串类型

TestString test = new TestString();

String testStr = test.toString();

System.out.println(testStr);