String 类 举例:
String s1 = "hello"; String s2 = "world"; String s3 = "hello";
system.out.println(s1 == s3); //true
s1 = new String("hello");
s2 = new String("hello");
system.out.println(s1 == s3); //false
system.out.println(s1.equals(s2)); //true
char c[] = {'s','u','n',' ','j','a','v','a'};
String s4 = new String(c);
system.out.println(s4); // sun java
String s5 = new String(c4,4,4);
system.out.println(s5); //java :从C4的第四个字节开始往后截四个字节。注意:别忘了第一位是0
public char charAt(int index)
返回字符串中第index个字符
public int length()
返回字符串的长度
public int indexOf(String str)
返回字符串中出现str的第一个位置
public int indexOf(String str,int fromIndex)
返回字符串中从fromIndex开始出现str的第一个位置
public boolean equelsIgnoreCase(String another)
比较字符串与another是否一样(忽略大小写)
public String replace(Char oldChar.Char newChar)
在字符串中用newChar字符替换oldChar字符
String s1 = "sun java"; String s2 = "sun Java";
System.out.println(s1.charAt(1)); // u
System.out.println(s2.length); // 8
System.out.println(s1.indexOf("java")); // 4 出现java的第一个位置
System.out.println(s1.indexOf("java")); // -1
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCae(s2)); // true 忽略大小写后是否一样
String s1 = "我是程序员 我在学Java";
String s2 = s1.replace("我","你");
System.out.println(s2); // 你是程序员,你在学Java
public boolean startsWith(String prefix);
判断字符串是否以prefix字符串开头
public boolean endWith(String suffix);
判断字符串是否以suffix字符串结尾
public string toUpperCase();
返回一个字符串为该字符串的大写形式
public string toLowerCase();
返回一个字符串为该字符串的小写形式
public string subString(int beginIndex);
返回该字符串从beginIndex开始到结尾的子字符串
public string subString(int beginIndex,int endIndex);
返回该字符串从beginIndex开始到endIndex结尾的子字符串
public string trim();
返回将改字符串去掉开头和结尾空格后的字符串