Java探索之string字符串的应用代码示例

2025-05-29 0 58

String类中提供了丰富的用于操作字符串的方法。

int indexOf(String str)

该方法用于返回当给定字符串在当前字符串中的位置,若当前字符串不包含给定字符串则返回-1。

重载的方法

int indexOf(String str,int formIndex),从指定下标处(包含)查询子串,返回返回当给定字符串在当前字符串中的位置,若当前字符串不包含给定字符串则返回-1。

用自己的算法实现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

28

29

30

31
package com.hz.practice;

/**

* 1. 用自己的算法实现startsWith和endsWith功能。

* @author ztw

*

*/

public class Practice01 {

public static void main(String[] args) {

String str = "qwewrewr";

// boolean temp = str.endsWith("r");

// System.out.println(temp);

/*

* startsWith

*/

char[] arr = str.toCharArray();

char c = 'r';

if(arr[0]==c){

System.out.println("true");

}else{

System.out.println("false");

}

/*

* endsWith

*/

if(arr[arr.length-1]==c){

System.out.println("true");

}else{

System.out.println("false");

}

}

}

输出结果:

?

1

2
false

true

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
package com.hz.practice;

import java.util.Scanner;

/**

* 2.采用字符的移位方式实现字符文本加密解密。

* @author ztw

*

*/

public class Practice02 {

public static void main(String[] args) {

System.out.println("请输入一个字符串");

Scanner sc =new Scanner(System.in);

String str1 = new String();

str1=sc.nextLine();

System.out.println(str1.replaceAll("a", "1.")

.replaceAll("b", "2.").replaceAll("c", "3.")

.replaceAll("d", "4.").replaceAll("e", "5.")

.replaceAll("f", "6.").replaceAll("g", "7.")

.replaceAll("h", "8.").replaceAll("i", "9.")

.replaceAll("j", "10.").replaceAll("k", "11.")

.replaceAll("l", "12.").replaceAll("m", "13.")

.replaceAll("n", "14.").replaceAll("o", "15.")

.replaceAll("p", "16.").replaceAll("q", "17.")

.replaceAll("r", "18.").replaceAll("s", "19.")

.replaceAll("t", "20.").replaceAll("u", "21.")

.replaceAll("v", "22.").replaceAll("w", "23.")

.replaceAll("x", "24.").replaceAll("y", "25.")

.replaceAll("z", "26."));

}

}

输出结果:

?

1

2

3
请输入一个字符串

asdsddffg

1.19.4.19.4.4.6.6.7.

3.验证是随机生成4位验证码,由用户输入并否输入正确, 如果输入错误就生成新的验证码让用户重新输入,最多输入5次!

?

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

44

45

46

47

48

49

50

51
package com.hz.practice;

import java.util.Random;

import java.util.Scanner;

/**

* 3.验证是随机生成4位验证码,由用户输入并否输入正确,

* 如果输入错误就生成新的验证码让用户重新输入,最多输入5次

* @author ztw

*

*/

public class Practice03 {

public static void main(String[] args) {

String str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char[]arr=new char[4];//定义一个长度是4的char型数组

Random sj=new Random();

System.out.println("验证码是:");

for(int i=0;i<4;i++)

{

arr[i]=str.charAt(sj.nextInt(61));//从str中随机截取4个单个字符并赋值给arr这个数组存放

}

System.out.println(arr);

Scanner sc=new Scanner(System.in);

System.out.println("请输入验证码");

String a=new String(arr);//把数组转换成字符串

//定义输入次数

for(int j=0;j<5;j++)

{

if(sc.nextLine().equals(a))

{

System.out.println("验证码输入正确");

}

else

{

System.out.println("验证码输入有误,请重新输入");

if(j<=3)

{

System.out.print("请输入验证码");

for(int i=0;i<4;i++)

{

arr[i]=str.charAt(sj.nextInt(61));//从str中随机截取4个单个字符并赋值给arr这个数组存放

}

System.out.println(arr);

a=new String (arr);

}

else

{

System.out.println("输入有误,对不起,5次机会已用完");

}

}

}

}

}

输出结果:

?

1

2

3

4

5
验证码是:

AVA8

请输入验证码

AVA8

验证码输入正确

?

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
package com.hz.practice;

/**

* 4.获取一个字符串在另一个字符串中出现的次数

思路:

1.定义一个计数器。

2.获取子串第一次出现的位置

3.从第一次出现的位置后剩余的字符串中继续获取子串出现的位置,每获取一次计数器加1

4,当获取不到时,计数完成

* @author ztw

*

*/

public class Practice04 {

public static void main(String[] args) {

String str1 = "asdasdas";

String str2 = "as";

//1.定义一个计数器。

int total = 0;

for (String temp = str1; temp!=null &&

temp.length()>=str2.length();) {

//2.获取子串第一次出现的位置

if(temp.indexOf(str2) == 0){

//3.从第一次出现的位置后剩余的字符串中继续获取子串出现的位置,每获取一次计数器加1

total ++;

}

temp = temp.substring(1);

System.out.print(temp+", ");

}

//4,当获取不到时,计数完成

System.out.println("计数完成:"+total);

}

}

输出结果:

?

1
sdasdas, dasdas, asdas, sdas, das, as, s, 计数完成:3

?

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
package com.hz.practice;

/**

* 5.获取两个子串中最大相同子串

思路:

1.将短的哪个子串按照长度递减的方式获取到

2.将每次获取到的子串去长串中判断是否包含,如果包含,已经找到

* @author ztw

*

*/

public class Practice05 {

public static String getMaxSubString(String s1,String s2) {

String max = "",min = "";

max = (s1.length()>s2.length())?s1: s2;

min = (max==s1)?s2: s1;

// sop("max="+max+"...min="+min);

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

{

for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++)

{

String temp = min.substring(y,z);

sop(temp);

if(max.contains(temp))//if(s1.indexOf(temp)!=-1)

return temp;

}

}

return "";

}

public static void main(String[] args)

{

String s1 = "ab";

String s2 = "cvhellobnm";

sop(getMaxSubString(s2,s1));

}

public static void sop(String str)

{

System.out.print(str+", ");

}

}

输出结果:

?

1
ab, a, b, b,

?

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

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58
package com.hz.practice;

import java.util.Scanner;

/**

* 6、写一个方法判断一个字符串是否对称

* @author ztw

*

*/

public class Practice06 {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

String str = input.next();//接收任意字符串

isOk1(str);

}

/**

* 判断字符串是否对称的方法(一)

* 通过取取索引对应值来进行一一比对

* @param str

*/

public static void isOk1(String str){

boolean result = true;

int count =(str.length()-1)/2;

for (int x=0;x<=count;x++ ){

if(str.charAt(x)!=str.charAt(str.length()-1-x)){

result = false;

break;

}

}

if(!result)

System.out.println("该字符串是不对称的");

else

System.out.println("该字符串是对称的");

}

/*

* public static void main(String[] args){

Scanner input = new Scanner(System.in);

String str = input.next();//接收任意字符串

if (isOk2(str)==true) {

System.out.println("真,对称!");

}else{

System.out.println("假,不对称!");

}

}

/**

* 方法二

* 通过String加强类中的取反方法reverse获取其逆向值

* 再与原字符串相比是否相等!

* 等于则返回TRUE,否则FALSE

* @param str

* @return

*/

/*

public static boolean isOk2(String str){

StringBuffer sb = new StringBuffer(str);

String str2 = sb.reverse().toString();

return str.equals(str2);

}

*/

}

输出结果:

?

1

2
asdsa

该字符串是对称的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.hz.practice;

/**

*

9、编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,

“To be or not to be",将变成"oT eb ro ton ot eb."。

* @author ztw

*

*/

public class Practice09 {

public static void main(String[] args) {

String str = "To be or not to be";

String[] str2 = str.split(" ");

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

char[] ci = str2[i].toCharArray();

System.out.print(" ");

for (int j = ci.length-1; j >= 0 ; j--) {

System.out.print(ci[j]);

}

}

}

}

?

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

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62
package com.hz.practice;

/**

* 10、已知字符串:"this is a test of java".

按要求执行以下操作:

(1) 统计该字符串中字母s出现的次数

(2) 取出子字符串"test"

(3) 用多种方式将本字符串复制到一个字符数组Char[] str中.

(4) 将字符串中每个单词的第一个字母变成大写, 输出到控制台。

(5) 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)

(6) 将本字符串转换成一个字符串数组,要求每个数组元素都是一个有意义的额英文单词,并输出到控制台

* @author ztw

*

*/

public class Practice10 {

public static void main(String[] args) {

String str = "this is a test of java";

// (1) 统计该字符串中字母s出现的次数

int count = 0;

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

if(str.charAt(i)=='s'){

count++;

}

}

System.out.println("字符串中字母s出现的次数:"+count);

// (2) 取出子字符串"test"

String str2 = (String) str.subSequence(10, 14);

System.out.println(str2);

// (3) 用多种方式将本字符串复制到一个字符数组Char[] str中.

char[] c = str.toCharArray();

System.out.println(c);

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

System.out.print(c[i]);

}

System.out.println();

// (4) 将字符串中每个单词的第一个字母变成大写, 输出到控制台。

str=str.toLowerCase();

String[] tt=str.split(" ");

System.out.println(tt.length);

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

{

//加个判断,长度大于0的

if(tt[i].length()>0){

System.out.print(String.valueOf(tt[i].charAt(0)).toUpperCase());

System.out.print(tt[i].substring(1)+" ");

}

}

System.out.println();

// (5) 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)

StringBuffer sb = new StringBuffer(str);

System.out.println(sb.reverse().toString());

char[] c3 = str.toCharArray();

for (int i = c3.length-1; i >= 0 ; i--) {

System.out.print(c3[i]);

}

System.out.println();

// (6) 将本字符串转换成一个字符串数组,要求每个数组元素都是一个有意义的额英文单词,并输出到控制台

String[] str5=str.split(" ");

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

System.out.print(str5[i]+", ");

}

}

}

输出结果:

?

1

2

3

4

5

6

7

8

9
字符串中字母s出现的次数:3

test

this is a test of java

this is a test of java

6

This Is A Test Of Java

avaj fo tset a si siht

avaj fo tset a si siht

this, is, a, test, of, java,

总结

以上就是本文关于Java探索之string字符串的应用代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/qq_33624284/article/details/53606519

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java探索之string字符串的应用代码示例 https://www.kuaiidc.com/113988.html

相关文章

发表评论
暂无评论