java string类练习_java String类常用方法练习小结

string 类代表字符串。java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。

字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。

string 类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、

创建字符串副本并将所有字符全部转换为大写或小写。

string类是最常用的类之一,下面就通过几个练习,熟悉string类中的提供的重要方法。

字符串练习一

给定一个字符串数组,按照字典顺序,进行大小写排序

思路:

1.对数组排序,可以用选择排序、冒泡排序等等。

2.for循环嵌套,比较,交换位置。

3.不同之处,以前比较的是数字,用的是比较运算符;

现在比较的是字符串对象,应该使用compareto方法。

public class stringtest_1

{

//对字符串数组进行排序

public static void stringsort(string[] arr)

{

//采用冒泡排序

for(int i=0;i

{

for(int j=0;j

{

//用compareto方法进行字符串比较

if(arr[j].compareto(arr[j+1])>0)

{

string temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

}

}

showarray(arr);

}

//定义方法,以[str1,str2,str3]的格式来打印数组

public static void showarray(string[] arr)

{

system.out.print("[");

for(int i=0;i

{

if(i!=arr.length-1)

system.out.print(arr[i]+",");

else

{

system.out.print(arr[i]+"]\n");

}

}

}

public static void main(string[] args)

{

string arr[]={"nba","abc","cba","zz","qq","haha"};

//打印数组

showarray(arr);

//对数组进行排序并输出

stringsort(arr);

}

}

运行:

66eeb609656b59f86773e721f1798628.png

字符串练习二

一个子串在字符串中出现的次数

思路:

1.用indexof方法获取子串在字符串中第一次出现的位置index

2.再用indexof方法获取以(index+子串长度)为起始的剩下的字符串中子串出现的位置,直到字符串中不再包含子串。可用while循环实现。

3.每次找到后用计数器记录即可。

public class stringtest_2

{

public static void main(string[] args)

{

string str="abcqwabcedcxabcuabcjkabcnmbabc";

//string str=null;

try

{

int count=countchildstr(str,"abc");

system.out.println("abc在"+str+"中出现的次数为:"+count);

}

catch (nullpointerexception ne)

{

system.out.println(ne);

}

catch(runtimeexception re)

{

system.out.println(re);

}

}

public static int countchildstr(string str,string key)

{

if(str==null||key==null)

{

throw new nullpointerexception("空指针异常,源字符串和子串都不能为null");

}

if(key=="")

{throw new runtimeexception("调用不合法,子串要有内容");}

int count=0,index=0;

while((index=str.indexof(key,index))!=-1)

{

count++;

index+=key.length();

}

return count;

}

}

8e859340aad97f45b2310f54fed438c0.png

字符串练习三

找到两个字符串的最大公共子串

思路:

1.判断较长字符串中是否包含较短字符串,如果包含,则较短字符串则为最大公共子串。

2.如果不包含,就对较短字符串以长度递减的方式取子串,去较长字符串中判断是否包含,如果包含就找到了,不用再找了。

3.重点:对字符串以长度递减的方式取子串

public class stringtest_3

{

public static void main(string[] args)

{

//创建两个不为空的字符串

string str1="abxczwsxcvdfas";

//string str1=null;

string str2="ghwsxcvxcdbgthnnnrfqwe";

try

{

string str=searchmaxcommonstr(str1,str2);

system.out.println("最大公共子串是:"+str);

}

catch (nullpointerexception ne)

{

system.out.println(ne);

}

}

public static string searchmaxcommonstr(string str1,string str2)

{

if(str1==null||str2==null)

throw new nullpointerexception("空指针异常,参数不能为null");

//断定较长字符串和较短字符串

string max=(str1.length()>str2.length())?str1:str2;

string min=(str1.equals(max))?str2:str1;

//按长度递减的方式取子串,从min.length~~1

for(int i=min.length();i>0;i--)

{

for(int x=0,y=x+i;y

{

string childstr=min.substring(x,y);

//若较长字符串中包含此子串,则找到了

//否则继续找

if(max.contains(childstr))

return childstr;

}

}

return null;

}

}

运行:

8f83ee1058b0457e0c72c4afc5ec4da5.png

字符串练习四

写一个和trim功能相同的方法

思路:

1.定义两个变量,用来存储两个角标

2.分别从头和尾遍历字符串,直到找到第一个不为空格的字符

3.截取字符串

public class stringtest_4

{

public static void main(string[] args)

{

string str=" abc ws ";

str=mytrim(str);

system.out.println(str);

}

public static string mytrim(string s)

{

int begin=0,end=s.length()-1;

//从头遍历

while(begin<=end && s.charat(begin)==' ')

{

begin++;

}

//从尾部遍历

while(begin<=end && s.charat(end)==' ')

{

end--;

}

return s.substring(begin,end+1);

}

}

运行:

5fe5e8b1e9ee6496a6783017eb036790.png

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持萬仟网!

希望与广大网友互动??

点此进行留言吧!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值