Java String 类
字符串在Java编程中广泛使用,字符串属于对象。一、编制字符串:
String str='abc ';
请注意,不能更改:String类。 创建String对象后,就不能更改其值。
如果需要更改字符串,则必须选择使用StringBuffer StringBuilder类。
二、字符串长度:
String类包含length ()方法,该方法返回字符串对象中包含的字符数(字符串的长度)。
String str='abcdefg ';
system.out.println (str .长度();
三、连接字符串:
String类提供了连接两个字符串的方法。
concat返回str2连接str1的新字符串。
ABC.Concat ('中国);
经常使用“”操作符连接字符串,如下所示。
abc,中国
四、字符串方法:
1 .字符(索引) )。
charAt ) )方法用于返回指定索引处的字符。 索引范围从0到length ()- 1。
字符串s=' abcde ';
Charresult=s.Charat(3;
系统输出打印(结果);
2 .将比较到(对象) )。
compareTo ) )方法用于比较两种方法。
字符串和对象的比较。
按词典顺序比较两个字符串。
语法:
比较到(对象) )。
或者
比较到(字符串)。
参数:
o --要比较的对象。
anotherString --要比较的字符串。
返回值:
如果参数字符串等于该字符串,则返回值为0;
如果字符串小于字符串参数,则返回小于0的值。
如果字符串大于字符串参数,则返回大于0的值。
String str1='String ';
String str2='String ';
String str3='Strings中国';
intresult=str1.comPareto(str2);
系统输出打印(结果);
result=str2.comPareto(str3;
系统输出打印(结果);
result=str3.comPareto(str1;
系统输出打印(结果);
3.intcomparetoignorecase (字符串)。
按字典顺序比较两个字符串,不论大小写。
4 .字符串连接(字符串)。
将指定的字符串连接到此字符串的末尾。
5 .布尔表达式(对象对象) )。
将此字符串与指定对象进行比较。
如果指定对象等于字符串,则为true; 否则返回false。
stringstr1=新字符串(ABC );
String Str2='abc ';
布林值=STR1.埃尔斯(STR2);
系统输出(ret sult );
6 .布尔型气候记忆库(字符串) )。
将此字符串与另一个字符串进行比较。 大小写没有关系。
7.byte[] getBytes (
getBytes ) )方法有两种形式。
getbytes(stringcharsetname ) :使用指定的字符集将字符串编码为字节数组,并将结果存储在新的字节数组中。
getBytes ) ) :使用平台的默认字符集将字符串编码为字节数组,并将结果存储在新的字节数组中。
stringstr1=新字符串(ABC );
特里
byte[] Str2=Str1.getBytes (;
system.out.println(str2;
str2=str1.getbytes(utf-8 );
system.out.println(str2;
str2=str1.GetBytes(iso-8859-1 );
系统。
out.println( Str2 );} catch ( UnsupportedEncodingException e){
System.out.println("不支持的字符集");
}
8.int hashCode()
返回此字符串的哈希码。
字符串对象的哈希码根据以下公式计算:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
使用 int 算法,这里 s[i] 是字符串的第 i 个字符,n 是字符串的长度,^ 表示求幂。空字符串的哈希值为 0。
String Str = new String("abc");
System.out.println( Str.hashCode() );
9.int indexOf(int ch)
返回指定子字符串在此字符串中第一次出现处的索引。
indexOf() 方法有以下四种形式:
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int indexOf(int ch, int index): 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str, int index): 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始,如果此字符串中没有这样的字符,则返回 -1。
参数:
ch -- 字符。
index -- 开始搜索的索引位置。
str -- 要搜索的子字符串。
String Str = new String("abcdefg");
System.out.println(Str.indexOf( 'o' ));
System.out.println(Str.indexOf( 'o', 3 ));
10.int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
lastIndexOf() 方法有以下四种形式:
public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int lastIndexOf(int ch, int fromIndex): 返返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1。
public int lastIndexOf(String str): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int lastIndexOf(String str, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1。
11.int length()
返回此字符串的长度。
12.boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
参数:
regex -- 匹配字符串的正则表达式。
String Str = new String("abcdefg");
System.out.println(Str.matches("(.*)def(.*)"));
13.String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
String Str = new String("hello");
System.out.println(Str.replace('o', 'T'));
14.String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串,成功则返回替换的字符串,失败则返回原始字符串。
15.String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
String Str = new String("ab-c-de-f");
byte b[]=Str.split("-");
for (String s: b){
System.out.println(s);
}
16.String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
参数:
regex -- 正则表达式分隔符。
limit -- 分割的份数。
17.String substring()
返回一个新的字符串,它是此字符串的一个子字符串。
语法:
public String substring(int beginIndex)
或
public String substring(int beginIndex, int endIndex)
参数:
beginIndex -- 起始索引(必填)。
endIndex -- 结束索引(不必填)。
String Str = new String("abcdefg");
System.out.println(Str.substring(4) );
System.out.println(Str.substring(3, 5) );
18.char[] toCharArray()
将此字符串转换为一个新的字符数组。
String Str = new String("abcde");
System.out.println( Str.toCharArray() )
19.String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
String Str = new String("ABC");
System.out.println( Str.toLowerCase() );
20.String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
21.String toString()
返回此对象本身。
String Str = new String("ABC");
System.out.println( Str.toString() );
22.String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
23.String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
24.String trim()
返回字符串的副本,忽略前导空白和尾部空白(删除头部和尾部空白符)。
String Str = new String(" abc ");
System.out.println( Str.trim() );
25.static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。
valueOf() 方法有以下几种不同形式:
valueOf(boolean b): 返回 boolean 参数的字符串表示形式。.
valueOf(char c): 返回 char 参数的字符串表示形式。
valueOf(char[] data): 返回 char 数组参数的字符串表示形式。
valueOf(char[] data, int offset, int count): 返回 char 数组参数的特定子数组的字符串表示形式。
valueOf(double d): 返回 double 参数的字符串表示形式。
valueOf(float f): 返回 float 参数的字符串表示形式。
valueOf(int i): 返回 int 参数的字符串表示形式。
valueOf(long l): 返回 long 参数的字符串表示形式。
valueOf(Object obj): 返回 Object 参数的字符串表示形式。
int a=1;
double b = 100.00;
char[] c = {'a', 'b', 'c', 'd', 'e'};
System.out.println(String.valueOf(a) );
System.out.println(String.valueOf(b) );
System.out.println(String.valueOf(c) );