字符串与字符数组的转换
String str1="hello";
char c[]=str1.toCharArray();
for(int i=0;i<c.length;i++)
{
System.out.print(c[i]+"\t");
}
System.out.println();
String str2=new String(c);
String str3=new String(c,0,3);
System.out.println(str2);
System.out.println(str3);
从字符串中取出指定位置的字符
String str1="hello";
System.out.println(str1.charAt(3));
字符串与byte数组的转换
String str1="hello";
byte b[]=str1.getBytes();
System.out.println(new String(b));
System.out.println(new String(b,1,3));
取得一个字符串的长度
String str1="hello";
System.out.println(str1.length());
查找一个指定的字符串是否存在
String str1="hello";
System.out.println(str1.indexOf("h"));
System.out.println(str1.indexOf("l",3));
System.out.println(str1.indexOf("a"));
去掉左右空格
String str1=" hello ";
System.out.println(str1.trim());
字符串截取
String str1="helloworld";
System.out.println(str1.substring(6));
System.out.println(str1.substring(0,5));
字符串拆分
String str1="hello world";
String c[]=str1.split("o");
for(int i=0;i<c.length;i++)
{
System.out.print(c[i]+"\t");
}
字符串大小写转换
- toUpperCase()
- toLowerCase()
String str1="hello world";
String str2="HELLO WORLD";
System.out.println(str1.toUpperCase());
System.out.print(str1.toLowerCase());
判断是否以指定的字符串开头或结尾
String str1="@hello world#";
System.out.println(str1.startsWith("@"));
System.out.print(str1.endsWith("#"));
不区分大小写的字符串比较
- equals()
- equalsIgnoreCase()
String str1="hello world";
String str2="hello WORLD";
System.out.println(str1.equals(str2));
System.out.print(str1.equalsIgnoreCase(str2));
将一个指定字符串替换成其他字符串
String str1="hello world";
System.out.println(str1.replaceAll("l","x"));
