String 类提供的成员执行以下操作:比较 String 对象;返回 String 对象内字符或字符串的索引;复制 String 对象的值;分隔字符串或组合字符串;修改字符串的值;将数字、日期和时间或枚举值的格式设置为字符串;对字符串进行规范化。

实现的接口

String 类分别用于实现 IComparableICloneableIConvertibleIEnumerableIComparable 接口。使用 Convert 类进行转换,而不是使用此类型的 IConvertible 显式接口成员实现。

 

以下内容为转载

String.IndexOf

String.IndexOf 方法 (Char, Int32, Int32)
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.IndexOf(value, startIndex, count)

参数
value:要查找的 Unicode 字符。 
startIndex:搜索起始位置。 
count:要检查的字符位置数。
返回值(Int32):
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。 


示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.IndexOf("中国").ToString();//返回 -1 
Label1.Text = str.IndexOf("盈基").ToString();//返回 3 
Label1.Text = str.IndexOf("盈基",10).ToString();//返回21 说明:这是从第10个字符开始查起。 
Label1.Text = str.IndexOf("邓",15,10).ToString();//返回 -1
Label1.Text = str.IndexOf("邓",15,20).ToString();//返回 -32 说明:从第15个字符开始查找,要查找的范围是从第15个字符开始后20个字符,即从第15-35个字符中查找。

String.LastIndexOf

String.LastIndexOf 方法
报告指定的 Unicode 字符或 String 在此实例中的最后一个匹配项的索引位置。

名称 说明  
String.LastIndexOf (Char) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。
String.LastIndexOf (String) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。
String.LastIndexOf (Char, Int32) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
String.LastIndexOf (String, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
String.LastIndexOf (String, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。一个参数指定要用于指定字符串的搜索类型。
String.LastIndexOf (Char, Int32, Int32) 报告指定的 Unicode 字符在此实例内的子字符串中的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.LastIndexOf (String, Int32, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.LastIndexOf (String, Int32, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。参数指定当前字符串中的起始搜索位置,以及要用于指定字符串的搜索类型。
String.LastIndexOf (String, Int32, Int32, StringComparison) 报告指定的 String 对象在此实例内的最后一个匹配项的索引位置。参数指定当前字符串中的起始搜索位置、要搜索的当前字符串中的字符数量,以及要用于指定字符串的搜索类型。



示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.LastIndexOf("邓文").ToString();//返回-1
Label1.Text = str.LastIndexOf("邓").ToString();//返回32

Label1.Text = str.LastIndexOf("邓",8).ToString();//返回-1
Label1.Text = str.LastIndexOf("邓",20).ToString();//返回14
Label1.Text = str.LastIndexOf("邓",33).ToString();//返回32
说明:在指定的范围内查找字符,这个范围是上面的输入的参数,理解为,从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置。示例中,0-8中没有“邓”字,所以返回-1,0-20范围中,有一个“邓”字在索引14位置上,0-33范围中有两个“邓”字,因为LastIndexOf是返回最后一个匹配项索引位置,所以返32,而不是14。

String.Substring

String.Substring 方法
从此实例检索子字符串。 

名称 说明
String.Substring (Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始。
String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。



示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文”
Label1.Text = str.Substring(11,7);//返回 “国际通邓事文*”

总结一下:

IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;
IndexOf是从左向右查,LastIndexOf是从右向左查,不管是IndexOf还是LastIndexOf,索引序列都是从左到右的(起始值是0)
Substring是字符串截取,返回值是一个截取后的字符串。

 

1、构造函数。

String() :构造一个空字符串对象。
String(byte[] bytes) :通过byte数组构造字符串对象。
String(byte[] bytes, int offset, int length) :通过byte数组,从offset开始,总共length长的字节构造字符串对象。
String(char[] value) :通过char数组构造字符串对象。
String(char[] value, int offset, int count) :通过char数组,从offset开始,总共length长的字节构造字符串对象。
String(String original) :构造一个original的副本。既,拷贝一个original。
String(StringBuffer buffer) :通过StringBuffer数组构造字符串对象;

byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};

String sb = new String(b);
String sb_sub = new String(b,3,2);
String sc = new String(c);
String sc_sub = new String(c,3,2);
String sb_copy = new String(sb);

System.out.println("sb: " + sb );
System.out.println("sb_sub: " + sb_sub );
System.out.println("sc: " + sc );
System.out.println("sc_sub: " + sc_sub );
System.out.println("sb_copy: " + sb_copy );
结果为:
sb: abcdefghij
sb_sub: de
sc: 0123456789
sc_sub: 34
sb_copy: abcdefghij

2、方法。
说明:
1. 所有方法均为public;
2. 书写格式:[修饰符] <返回类型> <方法名([参数列表])>
如:
static int parseInt(String s) 表示:此方法(parseInt)为类方法(static),返回类型为(int),方法所需参数为String类型。


1. char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。

String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
结果为:s.charAt(5): f

2. int compareTo(String anotherString) :当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
3. int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。

String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");

System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) );//返回长度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) );//返回'k'-'a'的差
结果为:
s1.compareTo(s2): 4
s1.compareTo(s3): 10


4. String concat(String str) :将该String对象与str连接在一起。
5. boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。
6. static String copyValueOf(char[] data) :
7. static String copyValueOf(char[] data, int offset, int count) :这两个方法将char数组转换成String,与其中一个构造函数类似。
8. boolean endsWith(String suffix) :该String对象是否以suffix结尾。

String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
结果为:s1.endsWith(s2): true


9. boolean equals(Object anObject) :当anObject不为空并且与当前String对象一样,返回true;否则,返回false。
10. byte[] getBytes() :将该String对象转换成byte数组。
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。

char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!");
s2.getChars(0,3,s1,7); //s1=I love you!
System.out.println( s1 );
结果为:I love you!

12. int hashCode() :返回当前字符的哈希表码。
13. int indexOf(int ch) :只找第一个匹配字符位置。
14. int indexOf(int ch, int fromIndex) :从fromIndex开始找第一个匹配字符位置。
15. int indexOf(String str) :只找第一个匹配字符串位置。
16. int indexOf(String str, int fromIndex) :从fromIndex开始找第一个匹配字符串位置。

String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
结果为:
s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12

17. int lastIndexOf(int ch)
18. int lastIndexOf(int ch, int fromIndex)
19. int lastIndexOf(String str)
20. int lastIndexOf(String str, int fromIndex) 以上四个方法与13、14、15、16类似,不同的是:找最后一个匹配的内容。
21. int length() :返回当前字符串长度。
22. String replace(char oldChar, char newChar) :将字符号串中第一个oldChar替换成newChar。
23. boolean startsWith(String prefix) :该String对象是否以prefix开始。
24. boolean startsWith(String prefix, int toffset) :该String对象从toffset位置算起,是否以prefix开始。

String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );
结果为:
s.startsWith(ss): true
s.startsWith(sss,6): true

25. String substring(int beginIndex) :取从beginIndex位置开始到结束的子字符串。
26.String substring(int beginIndex, int endIndex) :取从beginIndex位置开始到endIndex位置的子字符串。
27. char[] toCharArray() :将该String对象转换成char数组。
28. String toLowerCase() :将字符串转换成小写。
29. String toUpperCase() :将字符串转换成大写。

String s = new String("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
结果为:
s.toUpperCase(): JAVA.LANG.CLASS STRING
s.toLowerCase(): java.lang.class string

30. static String valueOf(boolean b)
31. static String valueOf(char c)
32. static String valueOf(char[] data)
33. static String valueOf(char[] data, int offset, int count)
34. static String valueOf(double d)
35. static String valueOf(float f)
36. static String valueOf(int i)
37. static String valueOf(long l)
38. static String valueOf(Object obj)

posted on 2012-12-10 22:54  陈谨  阅读(155)  评论(0编辑  收藏  举报