java 字符串截取的几种方式

来源:互联网 发布:北京it外包 编辑:程序博客网 时间:2024/05/17 18:46

1.split()+正则表达式来进行截取。
将正则传入split()。返回的是一个字符串数组类型。不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时。

String str = "abc,12,3yy98,0";String[]  strs=str.split(",");for(int i=0,len=strs.length;i<len;i++){    System.out.println(strs[i].toString());}

运行结果:

abc123yy980

2.通过subString()方法来进行字符串截取。
subString通过不同的参数来提供不同的截取方式
2.1只传一个参数
例如:

 String sb = "bbbdsajjds";    sb.substring(2);

将字符串从索引号为2开始截取,一直到字符串末尾。(索引值从0开始);
2.2传入2个索引值

String sb = "bbbdsajjds";sb.substring(2, 4);

从索引号2开始到索引好4结束(并且不包含索引4截取在内,也就是说实际截取的是2和3号字符);
运行结果如下:

bdsajjdsbd

举个例子,下面这个字符串,取出来MyCouponActivity

ios.YYVoucherViewController.android.MyCouponActivity?
JSONObject extrasJson = new JSONObject(extras);            Log.i("extrasJson",extrasJson.toString());            String back=extrasJson.optString("url");            String top=  back.substring(0,3);            Log.i("topff",top);            if(top.equals("app")){                String exittop=back.substring(6);                Log.i("exittop",exittop);                //  ios.YYVoucherViewController.android.MyCouponActivity?                String exitend= exittop.substring(0,exittop.indexOf("?"));                Log.i("exitend",exitend);                //ios.YYVoucherViewController.android.MyCouponActivity                String[] strs=exitend.split("\\.");                String togoclass=strs[3].toString();                  Intent  intent2 =new Intent(context, Class.forName("com.yylc.yylearncar.view.activity.mine."+togoclass));                context.startActivity(intent2);

通过class.forName的方法,跳转到界面上,但是必须是类的全路径名称

原创粉丝点击