java string类练习_java String类练习

1.设计一个方法 将字符串反转;ok-->ko

public class StringStudy{

public String reverse(String a){

int l=a.length();

char[] b=b=a.toCharArray();

for(int i=0;i

char temp=b[i];//a,b

b[i]=b[l-1-i];//d,c

b[l-1-i]=temp;//a,b

//System.out.println(i+":"+b[i]+"-"+b[l-1-i]);

}

a=new String(b);

return a;

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

String y="中国红";

y=x.reverse(y);

System.out.println(y);

}

}//1

3c6dde69ea9f2dcb756795b441882d75.png

2.设计一个方法 将给定字符串的正序和反序进行连接 ok-->okko

//1.设计一个方法 将字符串反转;ok-->ko

public class StringStudy{

public String reverse(String a){

int l=a.length();

char[] b=b=a.toCharArray();

for(int i=0;i

char temp=b[i];//a,b

b[i]=b[l-1-i];//d,c

b[l-1-i]=temp;//a,b

//System.out.println(i+":"+b[i]+"-"+b[l-1-i]);

}

a=new String(b);

return a;

}

//}

//2.设计一个方法 将给定字符串的正序和反序进行连接 ok-->okko

//public class StringStudy{

public String fp(String a){

/*

int l=a.length();

char[] b=a.toCharArray();

for(int i=0;i

char temp=b[i];

b[i]=b[l-1-i];

b[l-1-i]=temp;

}

String aa=new String(b);

a=a.concat(aa);

return a;

*/

return a.concat(this.reverse(a));

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

String y="abcd";

y=x.fp(y);

System.out.println(y);

}

}//2

60fa3f6987e263752af263ad261a7599.png

3.判断字符串是否是回文? abccba abcba

public class StringStudy{

public String reverse(String a){

int l=a.length();

char[] b=b=a.toCharArray();

for(int i=0;i

char temp=b[i];//a,b

b[i]=b[l-1-i];//d,c

b[l-1-i]=temp;//a,b

//System.out.println(i+":"+b[i]+"-"+b[l-1-i]);

}

a=new String(b);

return a;

}

public boolean isHuiWen(String a){

if(this.reverse(a).equals(a)){

return true;

}

return false;

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

String y="abcba";

boolean z=x.isHuiWen(y);

if(z){

System.out.println("是回文");

}else{

System.out.println("不是回文");

}

}

}//3

059ea6ee45b470833d3ded303cd57096.png

4.将给定的字符串右位移 x位置;(helloworld 2)-->ldhellowor

//4.将给定的字符串右位移 x位置;(helloworld 2)-->ldhellowor

public class StringStudy{

public String rightY(String a,int index){

String d="不能左移";

if(index>0){

index%=a.length();

String j=a.substring(0,a.length()-index);

String k=a.substring(a.length()-index);

d=k.concat(j);

}

return d;

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

String y="helloworld";

int z=1;

y=x.rightY(y,z);

System.out.println(y);

}

}//4

a5da93dcaa3039400d14e4123b336e0f.png

5.寻找若干字符串中最长的那个;ab,abcd-->abcd

//5.寻找若干字符串中最长的那个;ab,abcd-->abcd

public class StringStudy{

public String find(String...a){

String b=a[0];

int max=b.length();

for(int i=1;i

if(max

b=a[i];

max=b.length();

}

}

return b;

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

String j=x.find("abcdefgh","abc","e","jklkjljklljk");

System.out.println(j);

}

}//5

cc8bd2acdb441e36258ba1603a09c917.png

6.统计给定字母在字符串中出现的次数 "this is a a" "a"-->2

//6.统计给定字母在字符串中出现的次数 "this is a a test java" "a"-->4

public class StringStudy{

/*

public int find(String a,char b){

int x=0;

for(int i=0;i

if(a.charAt(i)==b){

x++;

}

}

return x;

}//方式1

*/

public int find(String a,char b){

String c=a.replaceAll(String.valueOf(b),"");

return a.length()-c.length();

}//方式2

}

public class Tests{

public static void main(String[] args){

//StringStudy z=new StringStudy();

StringStudy z=new StringStudy();

int m=z.find("this is a a test java",'a');

System.out.println(m);

}

}//6

75ca6b90a681244e7e9d3a92fe4dab40.png

7.将给定的字符串每一个首字母大写"this is a a"--> This Is A A

//7.将给定的字符串每一个首字母大写"this is a a"--> This Is A A

public class StringStudy{

/*

public String changeF(String a){

String result="";

String[] b=a.split(" ");

for(int i=0;i

String x=b[i].substring(0,1).toUpperCase();

String y=b[i].substring(1);

result=result.concat(x.concat(y)+" ");//最后一个字符多个空格

}

//result=result.substring(0,a.length());

result=result.trim();

return result;

}//方式1

*/

boolean flag=true;

public String changeF(String a){

char[] x=a.toCharArray();//转为数组(小写)

String b=a.toUpperCase();

char[] y=b.toCharArray();//数组(大写)

x[0]=y[0];

int index=a.indexOf(" ");//找寻a字符串出现索引的第一个位置

//2

int n=0;

while(flag){

if(index!=-1 && index

n+=index;//2,4

n++;

a=a.substring(index+1);//cd ef;ef;

x[n]=y[n];//C

index=a.indexOf(" ");//2

}else{

flag=false;

}

}

a=new String(x);

return a;//方式2

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

System.out.println(x.changeF("this is a test of java"));

}

}//7

45500b72b8798e2e57d4ef253a82d099.png

8.获取给定字符串中的全部数组"za8yq32"-->832

0c37bfca357ec009487faccbe818823d.png

//8.获取给定字符串中的全部数字"za8yq32"-->832

public class StringStudy{

public int check(String a){

String z="";

for(int i=0;i

if(a.codePointAt(i)>=48 && a.codePointAt(i)<=57){//0对应的code是48,9是57

//z+=a.substring(i,i+1);

z+=(char)a.codePointAt(i);

}

}

if(z.length()>0){

return Integer.parseInt(z);

}

return 0;

}

}

public class Tests{

public static void main(String[] args){

StringStudy x=new StringStudy();

System.out.println(x.check("za8yq32"));

}

}

c8a7bc8557fc3aad4ce1f95de7557874.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值