您的位置:首页 > 其它

String类常用方法。

2016-05-21 00:31 344 查看

一,字符数组与字符串。

一个字符串可以变成一个字符数组,同样,一个字符数组可以变成一个字符串。

在String类中提供了以下操作方法。

1)将字符串变成字符数组:public char[] toCharArray();

2) 字符数组变成字符串:public String(char [] value)

            public String(char[] value,int offset,int count);//表示从offset位置开始的count个字符,组合成为一个字符串。

public class StringAPIDemo01{
public static void main(String args[]){
String str1 = "hello" ;            // 定义字符串
char c[] = str1.toCharArray() ;    // 将一个字符串变为字符数组
for(int i=0;i<c.length;i++){    // 循环输出
System.out.print(c[i] + "、") ;
}
System.out.println("") ;        // 换行
String str2 = new String(c) ;    // 将全部的字符数组变为String
String str3 = new String(c,0,3) ;    // 将部分字符数组变为String
System.out.println(str2) ;        // 输出字符串
System.out.println(str3) ;        // 输出字符串
}
}
运行结果:
h,e,l,l,o
hello
hel


上面分别使用了字符串变数组,和数组变字符串两种方法。

二,从字符串中取出指定位置的字符。

如果要做这种操作,则返回类型肯定是char类型。

使用方法:public char charAt(int index)

public class StringAPIDemo02{
public static void main(String args[]){
String str1 = "hello" ;            // 定义String对象
System.out.println(str1.charAt(3)) ;    // 取出字符串中第四个字符
}
};
输出结果:
l


三,字符串与bety数组的转换。

byte数组,在一般I/O中经常用到

在String类中,提供以下方法进行字符串和字节数组的转换。

1)字符串变为字节数组:public byte getBytes();

2)讲一个字节数组变为字符串:

将全部数组变为字符串:public String(byte[] byte);

将部分数组变成字符串:public String(char[] byte,int offset,int count);

public class StringAPIDemo03{
public static void main(String args[]){
String str1 = "hello" ;            // 定义字符串
byte b[] = str1.getBytes() ;    // 将字符串变为byte数组
System.out.println(new String(b)) ;    // 将全部的byte数组变为字符串
System.out.println(new String(b,1,3)) ;    // 将部分的byte数组变为字符串
}
};


四,取得一个数组的长度。

public int length();

public class StringAPIDemo04{
public static void main(String args[]){
String str1 = "hello LiXingHua" ;        // 定义字符串变量
System.out.println("\""+str1+"\"的长度为:"+str1.length()) ;
}
};


五,查找指定字符串是否存在。

在实际操作中,经常会用到判断一个字符串是否存在某些内容,此时就可以使用以下方法:

1)从头开始查找:public int indexof(String str);返回字符串开始的下标。

2)从指定位置查找:public int indexof(String str,int fromIndex);//返回的是一个int数据,表示的是字符串的具体位置,如果没有,返回-1,

public class StringAPIDemo05{
public static void main(String args[]){
String str1 = "abcdefgcgh" ;                // 声明字符串
System.out.println(str1.indexOf("c")) ;        // 查到返回位置
System.out.println(str1.indexOf("c",3)) ;    // 查到返回位置,从第4个位置开始查找
System.out.println(str1.indexOf("x")) ;        // 没有查到返回-1
}
};
运行结果:
2,7,-1


六,去掉空格。

trim()可以去掉字符串左右的空格,但是字符串中间的空格是无法去掉的。

public class StringAPIDemo06{
public static void main(String args[]){
String str1 = "    hello    " ;        // 定义字符串
System.out.println(str1.trim()) ;    // 去掉左右空格后输出
}
};
运行结果:
hello


七,字符截取。

从一个字符串中取出部分内容,使用的方法是:

1)从指定位置开始截取到最后位置;public String substring(int beginIndex);

2)截取指定范围的位置内容:public String substring(int beginIndex,int endIndex);

public class StringAPIDemo07{
public static void main(String args[]){
String str1 = "hello world" ;        // 定义字符串
System.out.println(str1.substring(6)) ; // 从第7个位置开始截取
System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容
}
};
运行结果:
world
hello


八,拆分字符串。

如果现在需要按指定字符串去拆分一个字符串的话,则使用:public String[] split(String regex)

public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ;        // 定义字符串
String s[] = str1.split(" ") ;        // 按空格进行字符串的拆分
for(int i=0;i<s.length;i++){        // 循环输出
System.out.println(s[i]) ;
}
}
运行结果:
hello
world


例子按照空格,被拆分成两个字符串,形成一个包含两个字符串的字符串数组。

九,大小写转换。

将一个大写的字符串全部变成小写:public String toUpperCase();

讲一个小写的字符串全部变成大学:public String toLowerCase();

public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
}
运行结果:
HELLO WORLD
hello word


十,判断是否以指定的字符串开头或者结尾。

在String中可以以以下方法完成。

1)是否以指定字符串开头:public boolean starsWith(string prefix)

2)是否以指定字符串结尾:public boolean endWith(string suffix)

public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ;            // 定义字符串
String str2 = "HELLO**" ;            // 定义字符串
if(str1.startsWith("**")){            // 判断是否以“**”开头
System.out.println("(**HELLO)以**开头") ;
}
if(str2.endsWith("**")){            // 判断是否以“**”结尾
System.out.println("(HELLO**)以**结尾") ;
}
}
};
运行结果:

(**HELLO)以**开头

(HELLO**)以**结尾


十一,不区分大小写的字符串比较。

public boolean equalsIgnoreCase(String anotherString)


public class StringAPIDemo11{
public static void main(String args[]){
String str1 = "HELLO" ;            // 定义字符串
String str2 = "hello" ;            // 定义字符串
System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
+ str1.equalsIgnoreCase(str2)) ;    // 不区分大小写的比较
}
};
运行结果:
false
ture


十二,字符串替换。

使用这个方法完成字符串替换:

public String replaceAll(String regex,String replacement);

public class StringAPIDemo12{
public static void main(String args[]){
String str = "hello" ;            // 定义字符串
String newStr = str.replaceAll("l","x")    ;    // 现在将所有的l替换成x
System.out.println("替换之后的结果:" + newStr) ;
}
};
运行结果:
替换之后的结果:hexxo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: