当前位置:编程学堂 > String 常用方法总结

String 常用方法总结

  • 发布:2023-09-27 15:41

简单总结一下String常用的方法:

1.charAt(int index) 方法:

Returns the char value at the specified index. An index ranges from0 to length() - 1. The first char value of the sequence is at index0, the next at index 1, and so on, as for array indexing.

返回String 对应位置上的字符。

String s = "abc";

System.out.println(s.charAt(0));  //输出a

System.out.println(s.charAt(1));  //输出b

System.out.println(s.charAt(3));  //运行时出错 java.lang.StringIndexOutOfBoundsException: String index out of range: 3

 

2.codePointAt(int index) 方法:

Returns the character (Unicode code point) at the specified index. The index refers tochar values (Unicode code units) and ranges from 0 to length() - 1.

返回String对应位置上的字符的ascii码。

String s = "abc";

System.out.println(s.charAt(0));  //输出97

 

3.codePointBefore(int index)  方法:

Returns the character (Unicode code point) before the specified index. The index refers tochar values (Unicode code units) and ranges from 1 to length.

返回String对应位置上的前一个位置上的字符的ascii码。

String s = "abc";

System.out.println(s.charAt(1));  //输出97 注意是位置的前一个位置,不是字符的前一个字符,所以整个范围是从1到length

 

4.codePointCount(int beginIndex, int endIndex)方法:

Returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to thechar at index endIndex - 1. Thus the length (in chars) of the text range isendIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

codePointCount()准确计算unicode字符的数量,而不是char的数量。

这个方法有些复杂。。。。。。

 

5.compareTo(String anotherString) 方法:

两个字符串的比较:

String s = "abc";
String s1 = "aaa";
String s2 = "abcdef";
String s3 = "a";

假设k是两个字符串上第一个不相同的字符位置:分两种情况

1.如s.compareTo(s1); 此时k=1; 返回值就是:this.charAt(k)-anotherString.charAt(k) 也就是b-a = 1; 如果s换成"adc",返回值就是d-a=3了。

2.如s.compareTo(s2);此时k=3;s和s2的前半部份相等。返回值是:this.length()-anotherString.length() 也就是3-6 = -3,再如 s2.compareTo(s3) 返回值是5;

值得注意的是compareTo方法是区分字符的大小写的,如果String s="aBc"; 然后s.compareTo(s1),返回值就是B-a = -31 了。

 

6.compareToIgnoreCase(String str)方法:

Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of callingcompareTo with normalized versions of the strings where case differences have been eliminated by callingCharacter.toLowerCase(Character.toUpperCase(character)) on each character.

与compareTo方法不同的是不区分大小写。会调用大小写转换方法,先把所有字符变成小写,再进行比较。

 

7.concat(String str) 方法:

Concatenates the specified string to the end of this string.

If the length of the argument string is 0, then this String object is returned. Otherwise, a newString object is created, representing a character sequence that is the concatenation of the character sequence represented by thisString object and the character sequence represented by the argument string.

字符串的拼接。

String s = "abc";
String s1 = "def";

System.out.println(s.concat(s1)); //输出abcdef 

System.out.println(s.concat("")); //拼接个空字符串,返回原字符串

 

8.contains(CharSequence s) 方法:

Returns true if and only if this string contains the specified sequence of char values.

返回是否包含某一字符序列。返回值为true或者false;

String s = "abc";

s.contains("a"); //返回true

s.contains("d");//返回false

 

9.contentEquals(CharSequence cs) 方法:

Compares this string to the specified CharSequence. The result istrue if and only if this String represents the same sequence of char values as the specified sequence.

返回是否与参数字符序列相等。返回值为true或者false

String s = "abc";

s.contentEquals("abc"); //返回true

s.contentEquals("ddd");//返回false

 

10.endsWith(String suffix)

返回是否以suffix结尾。

String s = "abc";

s.endsWith("c"); //true

s.endsWith("b"); //false

 

11.equals(Object anObject)  and  equalsIgnoreCase(String anotherString)

Compares this string to the specified object. The result is true if and only if the argument is notnull and is a String object that represents the same sequence of characters as this object.

字符串相等比较


12 indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that: 

包含特定字符串的第一个位置。

String s = "abcdcd";

System.out.println(s.indexOf("d")); //返回3




 


 

 

 

 

 

相关文章