String类的获取功能、转换功能

2025-05-29 0 35

String的获取功能:String的基本获取功能、获取功能的举例子、String的基本转换功能、转换功能的举例子、

1、String的获取功能:

(1)int length()

获取字符串的长度,即字符串中字符的个数。

(2)char charAt(int index)

获取指定索引位置上的字符。

(3)int indexOf(int ch)

获取指定字符在此字符串中第一次出现的索引。注意:这里用的是int,不是char,原因是'a'和97都可以作为实参传入。

(4)int indexOf(String str)

获取指定字符串在此字符串中第一次出现的索引。

(5)int indexOf(int ch,int fromIndex)

获取指定字符在此字符串中指定位置后第一次出现的索引。

(6)int indexOf(String str,int fromIndex)

获取指定字符串在此字符串中指定位置后第一次出现的索引。

(7)String substring(int start)

从指定位置截取子字符串,默认是截取到末尾。(包含start位置)

(8)String substring(int start,int end)

从指定位置开始到指定位置结束截取子字符串。(包start不包end)

2、获取功能的举例

?

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
package cn.itcast_06;

public class StringDemo {

public static void main(String[] args) {

// int length()

// 获取字符串的长度,即字符串中字符的个数。

String s="helloworld";

System.out.println("length():"+s.length());//10

System.out.println("--------------");

// char charAt(int index)

// 获取指定索引位置上的字符。

System.out.println("charAt:"+s.charAt(0));//h

System.out.println("charAt:"+s.charAt(9));//d

System.out.println("--------------");

// int indexOf(int ch)

// 获取指定字符在此字符串中第一次出现的索引。注意:这里用的是int,不是char,

// 原因是'a'和97都可以作为实参传入。

System.out.println("indexOf:"+s.indexOf('h'));//0

System.out.println("indexOf:"+s.indexOf('d'));//9

System.out.println("--------------");

// int indexOf(String str)

// 获取指定字符串在此字符串中第一次出现的索引。

System.out.println("indexOf:"+s.indexOf("owo"));//4

System.out.println("indexOf:"+s.indexOf("ld"));//8

System.out.println("--------------");

// int indexOf(int ch,int fromIndex)

// 获取指定字符在此字符串中指定位置后第一次出现的索引。

// int indexOf(String str,int fromIndex)

// 获取指定字符串在此字符串中指定位置后第一次出现的索引。

System.out.println("indexOf:"+s.indexOf('l',4));//8

System.out.println("indexOf:"+s.indexOf('l',40));//-1

System.out.println("--------------");

// String substring(int start)

// 从指定位置截取子字符串,默认是截取到末尾。(包含start位置)

System.out.println("substring:"+s.substring(4));//oworld

System.out.println("substring:"+s.substring(0));//helloworld

// String substring(int start,int end)

// 从指定位置开始到指定位置结束截取子字符串。(包start不包end)

System.out.println("substring:"+s.substring(4,8));//owor

System.out.println("substring:"+s.substring(0,s.length()));//helloworld

}

}

3、String转换功能:

(1)byte[ ] getBytes( )

(2)char[ ] toCharArray( )

把字符串转换为字符数组。

(3)static String valueOf(char[ ] chs)

把字符数组转成字符串。

(4)static String valueOf(int i)

把int型的数据转成字符串。

注意:String的valueOf方法可以把任意型的数据转成字符串。

(5)String toLowerCase( )

把字符串转成小写。

(7)String toUpperCase( )

把字符串转成大写。

(8)String concat(String str)

把字符串拼接。用 + 也可以。

4、String转换功能举例:

?

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

42

43
package cn.itcast_06;

public class StringDemo4 {

public static void main(String[] args) {

// 定义一个字符串对象

String s = "JavaSE";

// byte[] getBytes():把字符串转换为字节数组。

byte[] bys = s.getBytes();

for (int x = 0; x < bys.length; x++) {

System.out.println(bys[x]);

}

System.out.println("----------------");

// char[] toCharArray():把字符串转换为字符数组。

char[] chs = s.toCharArray();

for (int x = 0; x < chs.length; x++) {

System.out.println(chs[x]);

}

System.out.println("----------------");

// static String valueOf(char[] chs):把字符数组转成字符串。

String ss = String.valueOf(chs);

System.out.println(ss);

System.out.println("----------------");

// static String valueOf(int i):把int类型的数据转成字符串。

int i = 100;

String sss = String.valueOf(i);

System.out.println(sss);

System.out.println("----------------");

// String toLowerCase():把字符串转成小写。

System.out.println("toLowerCase:" + s.toLowerCase());

System.out.println("s:" + s);

System.out.println("----------------");

// String toUpperCase():把字符串转成大写。

System.out.println("toUpperCase:" + s.toUpperCase());

System.out.println("s:" + s);

System.out.println("----------------");

// String concat(String str):把字符串拼接。

String s1 = "JavaSE";

String s2 = "JavaEE";

String s3 = s1 + s2;

String s4 = s1.concat(s2);

System.out.println("s3:"+s3);

System.out.println("s4:"+s4);

}

}

补充:

下面介绍下String的获取功能

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
package string;

//String类的获取功能

public class StringDemo {

public static void main(String[] args) {

//定义一个字符串对象

String s="helloworld";

//返回字符串的长度

System.out.println("s.length="+s.length());

//获取指定位置的索引字符

System.out.println("charAt:"+s.charAt(7));

// 返回指定字符在此字符串中第一次出现处的索引

System.out.println("indexOf:"+s.indexOf('l'));

//返回指定字符串 在此字符串中第一次出现处的索引

System.out.println("indexOf:"+s.indexOf("owo"));

//返回指定字符在此字符串中从指定位置后第一次出现的索引

System.out.println("indexOf:"+s.indexOf('l',4));//找不到或者不存在都是返回-1

//返回指定字符串在此字符串中从指定位置后第一次出现的索引

System.out.println("indexOf:"+s.indexOf("ell",4));

//从指定位置到末尾开始截取

System.out.println("substring:"+s.substring(2));

//从指定位置到指定位置结束截取(前闭后开)

System.out.println("substring:"+s.substring(2,8));

}

}

总结

以上所述是小编给大家介绍的String的获取功能、转换功能 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://blog.csdn.net/cmm0401/article/details/79969277

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 String类的获取功能、转换功能 https://www.kuaiidc.com/111845.html

相关文章

发表评论
暂无评论