java substring替换_JAVA字符串简单处理--substring,replace,split

1.新建一个字符串数组

例如:

String[] str={"222","555","111"};

String[] str2=new String[3];

str2[0]="22";

str2[1]="44";

str2[2]="33";

System.out.print("str字符串:");

for(int i=0;i

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

}

System.out.print("\nstr2字符串:");

for(int i=0;i

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

}

输出:

str字符串:222,555,111,

str2字符串:22,44,33,

2.substring

例如:

String str="111,222,444,";

str=str.substring(0, str.length()-1);

System.out.println("去掉最后一个字符:"+str);

String strPart="13503009999".substring(1,5);

System.out.println("字符串截取:"+strPart);

输出:

去掉最后一个字符:111,222,444

字符串截取:3503

备注:1:表示起始位置,从0开始;5:表示结尾位置,从0开始数,不包括5

3.repalce

例如:

String str="2018-10-23";

System.out.println("relpace替换后:"+str.replace('-', '/'));

System.out.println("relpace替换后:"+str.replace("-", ""));

System.out.println("relpaceAll替换后:"+str.replaceAll("-23", ""));

输出:

relpace替换后:2018/10/23

relpace替换后:20181023

relpaceAll替换后:2018-10

4.split

例如:

public void test() {

outPrint(",qwe",",qwe".split(","));

outPrint("qwe","qwe".split(","));

outPrint("qwe,","qwe,".split(","));

}

public void outPrint(String strName,String[] str){

System.out.print(strName+"分解后的长度为:"+str.length+": ");

for(int i=0;i

System.out.print("第"+(i+1)+"个元素为:"+str[i]);

}

System.out.println();

}

输出:

,qwe分解后的长度为:2: 第1个元素为:第2个元素为:qwe

qwe分解后的长度为:1: 第1个元素为:qwe

qwe,分解后的长度为:1: 第1个元素为:qwe

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值