Java笔记—String类常用方法
String类的对象创建后不能修改,用其方法“修改”的字符串其实是生成了一个新的String对象并返回,原来的String对象仍存在且未被修改
获取、查找方法
public char charAt(int index) // 以char返回指定索引处的字符
String str = "Change the world by program";
char result = str.charAt(2);
System.out.println(result); //a
public int length() //获取字符串长度
String str = "Change the world by program";
System.out.println(str.length()); //27
public String substring(int beginIndex) 或 public String substring(int beginIndex, int endIndex)
//返回指定索引值之间的新字符串
String str = "Change the world by program";
String str2 = str.substring(7);
String str3 = str.substring(7,16);
System.out.println(str2); //the world by program
System.out.println(str3); //the world
public int indexOf(int ch) //返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(int ch, int fromIndex) //从fromIndex处开始搜索
public int indexOf(String str) //返回指定字符串在此字符串中第一次出现的索引,返回的是首字母位置。
public int indexOf(String str, int fromIndex) /从fromIndex处开始搜索
//以上方法若未能成功查找,则返回-1
另:int lastIndexOf(int ch)表示反向搜索
String str = "Change the world by program";
//正向查找
System.out.println(str.indexOf('a')); //2
System.out.println(str.indexOf('a',10)); //25
System.out.println(str.indexOf("program")); //20
System.out.println(str.indexOf("program",21)); //-1
//反向查找
System.out.println(str.lastIndexOf('a')); //25
System.out.println(str.lastIndexOf('a',10)); //2
System.out.println(str.lastIndexOf("program")); //20
System.out.println(str.lastIndexOf("program",19)); //-1
public String concat(String str) //将指定的字符串连接到该字符串的末尾
String str = "Change the world by program";
System.out.println(str.concat(" ~tail")); //Change the world by program ~tail
//若参数字符串长度为0,则返回原来的String对象
public String toLowerCase() //将字符串转化为小写
public String toUpperCase() //将字符串转化为大写
String str1 = "CHANGE THE WORLD BY PROGRAM";
String str2 = "Change the world by program";
System.out.println(str1.toLowerCase());
//change the world by program
System.out.println(str2.toUpperCase());
//CHANGE THE WORLD BY PROGRAM
public String replace(char oldChar, char newChar) //用字符newChar全部替换字符串中的字符oldChar
public String replace(CharSequence target, CharSequence replacement) //用字符串replacement替换原字符串中的字符串target
String str = "Change the world by program";
System.out.println(str.replace('a', 'k'));
//Chknge the world by progrkm
System.out.println(str.replace("the", "a"));
//Change a world by program
public String replaceAll(String regex, String replacement) //用字符串replacement替换给定正则表达式(regex)匹配的所有子字符串
public String replaceFirst(String regex, String replacement) //用字符串replacement替换给定正则表达式(regex)匹配的第一个子字符串
String str = "Change the world by program";
System.out.println(str.replaceAll(".h.", "EAE"));
//EAEnge EAE world by program
System.out.println(str.replaceFirst(".h.", "EAE"));
//EAEnge the world by program
public String trim() //删除字符串前后的空白符
String str = " Change the world by program ";
System.out.println(str.trim());
//Change the world by program
public String[] split(String regex, int limit) //以匹配给定正则表达式分割字符串,以数组形式返回
String str = "Change the world by program"; String[] result = str.split("a|o"); System.out.println(Arrays.toString(result)); //[Ch, nge the w, rld by pr, gr, m]
String[] result2 = str.split("a|o",3); //限制份数 System.out.println(Arrays.toString(result2)); //[Ch, nge the w, rld by program]
public char[] toCharArray() //将字符串转化为一个新的字符数组
String str = "Change the world by program";
char[] result = str.toCharArray();
System.out.println(Arrays.toString(result));
//[C, h, a, n, g, e, , t, h, e, , w, o, r, l, d, , b, y, , p, r, o, g, r, a, m]
一些判断方法
public boolean equals(Object anObject) //进行比较,如果给定对象与字符串相等,则返回 true;否则返回 false
public boolean equalsIgnoreCase(String anotherString) //将此字符串与另一字符串比较,忽略大小写
String str = "Change the world by program";
String str2 = "Change the world by program";
String str3 = "CHANGE THE WORLD BY PROGRAM";
System.out.println(str.equals(str2)); //true
System.out.println(str.equals(str3)); //false
System.out.println(str.equalsIgnoreCase(str3)); //true
public boolean contains(CharSequence s) //当且仅当此字符串包含指定的字符串时才返回true
String str = "Change the world by program";
System.out.println(str.contains("Change")); //true
System.out.println(str.contains("change")); //false
public boolean endsWith(String suffix) //判断此字符串是否以指定的后缀结尾
public boolean startsWith(String prefix) //判断此字符串是否以指定的前缀开始
String str = "Change the world by program";
System.out.println(str.endsWith("program")); //true
System.out.println(str.endsWith("by")); //false
System.out.println(str.startsWith("Change")); //true
System.out.println(str.startsWith("the")); //false
System.out.println(str.startsWith("the",7)); //也可指定开始查找的位置,因此是true
public int compareTo(String anotherString) //将此字符串与目标字符串按字典顺序进行比较,根据大小返回不同的值
String str = "ChangE the world by program";
String str2 = "ChangG the world by program";
String str3 = "ChangA the world by program";
String str4 = "ChangE the world by program";
System.out.println(str.compareTo(str2)); //比给定字符串小,返回负数-2
System.out.println(str.compareTo(str3)); //比给定字符串大,返回正数4
System.out.println(str.compareTo(str4)); //与给定字符串相等,返回0
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
public boolean regionMatches(int toffset, String other, int ooffset, int len)
//比较两个字符串在一段区域内是否相等 ignoreCase若为true,则忽略大小写
String str = "Change the world by program";
String other = "How beautiful the world is";
System.out.println(str.regionMatches(7, other, 14, 9)); //true
//第一个参数toffset为此字符串中子区域的起始偏移量
//第二个参数other为进行比较的字符串
//第三个参数为other中子区域的起始偏移量
//第四个参数len为比较区域长度
浙公网安备 33010602011771号