好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java字符串的重要使用方法以及实例

1.返回string[长度]方法

你如何确定给定string的长度?java提供了一种称为[length()]的方法。将它用于您需要查找string的长度。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class str_sample {

 

   public static void main(string[] args){

 

     //测试string长度的方法

 

     string name= "hello work" ;

 

     //length方法返回的是整数

 

     int num=name.length();

 

     system.out.println( "字符串的长度:" +num);

 

   }

 

}

输出结果:

字符串长度:10

2.字符串[indexof()]方法

我怎么能找到哪个角色在哪个位置?

indexof]可以帮助你指定的特定字符第一次出现的位置,若找不到返回-1

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

      int num=name.indexof( "java" );

 

      system.out.println( "java第一次出现的位置:" +num);

 

   }

 

}

输出结果:

java第一次出现的位置:7

 

3.字符串[lastindexof()]方法

如果我知道长度,我想从字符串后面查找角色在哪个位置?

[lastindexof]可以从指定位置开始反向检索,返回最后出现你指定的特定字符的位置,若找不到返回-1

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     //name的长度

 

     int num=name.length();

 

       //lastindexof(指定特殊字符,指定位置)

 

        int index=name.lastindexof( "java" ,num);

 

         system.out.println( "java最后一次出现的位置:" +index);

 

     

 

   }

 

]

输出结果:

java最后一次出现的位置:29

4.字符串[substring()]方法

如果我只想要字符串中的一段,那我该怎么办呢?

[substring]可以从指定的头和为截取字符串,返回截取后的字符串。注意:java中表示范围都是含头不含尾。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     //截取how这个字母,首先你要知道h的下标,可使用之前的indexof方法

 

     int num=name.indexof( "h" );

 

     //由于java中示范围都是含头不含尾,所以要多加一位

 

     string str=name.substring(num,num+ 3 );

 

     system.out.println(str);

 

     //也可从指定位置直接截取到字符串尾部

 

     string str2=name.substring(num);

 

     system.out.println(str2);

 

   }

 

]

输出结果:

how

how do you like java?

5.字符串[charat()]方法

怎么能根据位置获取字符呢?

[chatat]可以帮到你,用于返回指定下标的字符

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     //创建循环遍历name的下标

 

     for ( int i= 0 ;i<name.length();i++){

 

       //将下标放入charat方法中

 

       char ch=name.charat(i);

 

       system.out.print(ch);

 

     }

 

   }

 

]

输出结果:

i like java. how do you like java?

6.字符串[startswith(),endswith()]方法

怎么判断字符串是以什么开头或什么结束的呢?

[startswith()],检查字符串是否以指定字符串开始。[endswith()]检查字符串是否以指定字符串结尾

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     //是否以[i]开头,正确返回true,否则返回false

 

     boolean s1=name.startswith( "i" );

 

     //startswith()第二种用法,可判断指定位置是否是指定字符串

 

     boolean s2=name.startswith( "java" , 7 );

 

     //判断字符串是否以[?]结尾

 

     boolean e1=name.endswith( "?" );

 

     system.out.println( "是否以[i]开头:" +s1); 

 

     system.out.println( "位置7是否是[java]开头:" +s2);

 

     system.out.println( "是否以[?]结尾:" +e1);

 

    }

 

}

输出结果:

否以[i]开头:true

位置7是否是[java]开头:true

是否以[?]结尾:true

7.字符串[compareto()]方法

"compareto"它从第一位开始比较, 如果遇到不同的字符,则马上返回这两个字符的ascii值差值.返回值是int类型。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

public class str_sample{

 

   public static void main(string[] args){

 

     //a的ascli值为65,a的ascli值为97

 

     string a= "a" ;

 

     string b= "a" ;

 

     string c= "aa" ;

 

     string d= "abc" ;

 

     string e= "ad" ;

 

     int num=a.compareto(b);

 

     //还有一种方法忽略大小写进行比较

 

     int num2=a.comparetoignorecase(b);

 

     //长度不一样且前几个字符也不一样,从第一位开始找,当找到不一样的字符时,则返回的值是这两个字符比较的值

 

     int num3=c.compareto(d);

 

     //如多个字符,第一个字符相同则直接比较第二个字符,以此类推

 

     int num4=e.compareto(c);

 

     system.out.println( "a与b比较:" +num);

 

     system.out.println( "a与b比较(忽略大小写):" +num2);

 

     system.out.println( "c与d比较:" +num3);

 

     system.out.println( "e与d比较:" +num4);

 

    }

 

}

输出结果:

a与b比较:-32

a与b比较(忽略大小写):0

c与d比较:-1

e与d比较:3

8.字符串[contains()]方法

如果你想知道字符串中是否包含你想要的字符串?

那么[contanins]可以满足你的需求,判断是否包含指定字符串

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     //判断是否包含[you]这个字符串

 

     boolean bl=name.contains( "you" );

 

     system.out.println( "name字符串中是否包含[you]:" +bl);

 

   }

 

]

输出结果:

name字符串中是否包含[you]:true

9.字符串[replace()]方法

您可以指定要替换的字符串部分以及参数中的替换字符串。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     string str=name.replace( "java" , "php" );

 

     system.out.println( "替换前:" +name);

 

     system.out.println( "替换后:" +str);

 

   }

 

]

输出结果:

替换前:i like java. how do you like java?

替换后:i like php. how do you like php?

10.字符串[tolowercase()]和[touppercase()]方法

[tolowercase()]将字符串以小写形式显示,touppercase()]将字符串以大写形式显示。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class str_sample{

 

   public static void main(string[] args){

 

     string name= "i like java. how do you like java?" ;

 

     string low=name.tolowercase();

 

     string upp=name.touppercase();

 

     system.out.println( "小写显示:" +low);

 

     system.out.println( "大写显示:" +upp);

 

   }

 

]

输出结果:

小写显示:i like java. how do you like java?

大写显示:i like java. how do you like java?

 

查看更多关于java字符串的重要使用方法以及实例的详细内容...

  阅读:19次