java面向对象中的String类中12种常用的方法

1、字符串与字符数组的转换

       字符串可以使用toCharArray()方法变成一个字符数组,也可以使用String类的构造方法把一个字符数组变成一个字符串。

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.println(c[i] + "\t");
		}

		System.out.println("");
		String str2 = new String(c); //将全部字符数组变为String
		String str3 = new String(c,0,3); //将部分字符数组变为String
		System.out.println(str2);
		System.out.println(str23);

	}
}

程序运行结果:
h e l l o
hello
hel

2、从字符串中取出指定位置的字符

      直接使用String类中的charAt()方法取出字符串指定位置的字符,例如。

public class StringAPIDemo02
{
	public static void main(String[] args) 
	{
	String str1 = "hello";
	System.out.println(str1.charAt(3)); //取出字符串中第4个字符
   
	}
}

程序运行结果:
l

3、把一个字符串变成一个byte数组,也可以把一个byte数组变成一个字符串。

     字符串可以通过getBytes()方法将String变为一个byte数组,然后可以通过String的构造方法将一个字节数组重新变为字符串,例如:

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数组变为字符串
   
	}
}

程序运行结果:
hello
ell

4、取得一个字符串的长度

      在String中使用length()方法取得字符串的长度,例如:

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

程序运行结果:
"hello chenjunlin" 的长度为:15

注:length与length()区别,在数组操作中,使用length取得数组的长度,但是操作的最后没有(),而字符串调用length是一个方法,只要是方法后面都有“()”。

5、查找一个指定的字符串是否存在

     在String中使用indexOf()方法,可以返回指定的字符串位置,如果不存在则返回-1,例如:

public class StringAPIDemo05
{
	public static void main(String[] args) 
	{
	String str1 = "chenjunlin";
	System.out.println(str1.indexOf("c")); //查找返回位置
	System.out.println(str1.indexOf("c",3)); //查到返回位置,从第4个开始查找
	System.out.println(str1.indexOf("x")); //没有查到返回-1
   
	}
}

6、去掉左右空格

      在开发过程中,用户输入的数据中可能含有大量的空格,使用trim()方法可以去掉字符串左右空格,例如:

public class StringAPIDemo06
{
	public static void main(String[] args) 
	{
	String str1 = "    chenjunlin   ";
	System.out.println(str1.trim()); //查找返回位置
	//System.out.println(str1.indexOf("c",3)); //查到返回位置,从第4个开始查找
	//System.out.println(str1.indexOf("x")); //没有查到返回-1
   
	}
}

7、字符串截取

      在String中提供了两个substring()方法,一个是从指定位置截取到字符串结尾,另一个是截取指定范围内的内容,例如:

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

8、按照指定的字符串拆分字符串

      在String中通过split()方法可以进行字符串的拆分操作,拆分的数据将以字符串数组的形式返回,例如:

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

9、字符串的大小写转换

      在用户输入信息是,有时需要统一输入数据的大小写,此时使用toUpperCase()和toLowerCase()两个方法完成大小写的转换操作,例如:

10、判断是否以指定的字符串开头和结尾

       在String中使用startsWith()方法可以判断字符串是否以指定的内容开头,使用endsWith()方法可以判断字符串是否以指定的内容结尾,例如:

11、不区分大小写进行字符串比较

        在String中可以通过equals()方法进行字符串内容的比较,但这种比较方法是区分大小写的比较,如果要完成不区分大小写的比较可以使用equalsIgnoreCase()方法,例如:

12、将一个指定的字符串替换成其他的字符串

         使用string的replaceAll()方法可以将字符串的指定内容进行替换,例如:



 


posted @ 2014-09-28 16:09  Doctor.chen  阅读(439)  评论(0编辑  收藏  举报