String 常用方法总结
2014-08-19 16:25
423 查看
简单总结一下String常用的方法:
1.charAt(int index) 方法:
Returns the
返回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 to
返回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 to
返回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
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 calling
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
by this
字符串的拼接。
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
返回是否与参数字符序列相等。返回值为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 not
字符串相等比较
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
1.charAt(int index) 方法:
Returns the
charvalue at the specified index. An index ranges from
0to
length()- 1. The first
charvalue of the sequence is at index
0, 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 to
charvalues (Unicode code units) and ranges from
0to
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 to
charvalues (Unicode code units) and ranges from
1to
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
beginIndexand extends to the
charat index
endIndex- 1. Thus the length (in
chars) of the text range is
endIndex-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 calling
compareTowith normalized versions of the strings where case differences have been eliminated by calling
Character.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
Stringobject is returned. Otherwise, a new
Stringobject is created, representing a character sequence that is the concatenation of the character sequence represented
by this
Stringobject 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 is
trueif and only if this
Stringrepresents 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 not
nulland is a
Stringobject 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
相关文章推荐
- 复习总结C#中的string的常用容易混乱的方法
- OC总结----类的方法.继承.NSString类的常用方法.NSMutableString 类的方法.数组类.字典类.block语法以及数组排序高级.属性.category (分类,类目).延展,等
- String 类中常用方法总结
- String字符串常用方法总结
- String与StringBuffer常用方法总结
- String和StringBuffer 常用方法总结
- STD中string的常用方法总结
- String 及 StringBuffer常用方法总结
- GUAVA常用方法总结整理(list map string concurrent file)
- 学习中 常用到的string内置对象方法的总结
- Ruby中有用但不常用的String方法总结
- String常用方法小总结
- String 常用方法最优算法实现总结 (二)
- iOS NSMutableAttributedString常用方法总结
- String 常用方法最优算法实现总结 (三) -- findCommonSubstring 和difference
- STD中string的常用方法总结[zz]
- string类型常用方法总结
- STD中string的常用方法总结
- java基础知识1--String常用方法总结
- javascript中数组(Array)对象和字符串(String)对象的常用方法总结