字符串信息查找、获取、截取、替换转换、分割
2016-03-01 15:57
281 查看
//比较字符串是否相等。 System.out.println(a.equals(b)); System.out.println(b.equals(c)); //字符串信息 a.length();//带()的是方法,不带的是属性 //indexOf(),只能从前往后找: //查找字符串位置,查找字符串中子字符串的位置,返回值是找到之后的索引值。 System.out.println(a.indexOf("串"));//索引的位置。 System.out.println(a.indexOf("符串"));//首字的索引值 //查找不到时: System.out.println(a.indexOf("无"));//返回值是-1 //有多个时,只找第一个所在的索引值: String g=new String("字符串字符串字符串"); System.out.println(a.indexOf("串")); //lastIndexOf();从后边往前找 System.out.println(g.lastIndexOf("符")); //获取字符.charAt(索引) char c1=g.charAt(2); System.out.println(c1); //判断字符串开始 1,g.starswith("")。。 //2,利用indexOf。判断第一个出现该字符串的索引是不是等于0 System.out.println(g.startsWith("字符"));//返回值是bool值。 System.out.println(g.indexOf("字符")==0);//返回值是bool值. //判断字符串结束,1,g.endsof("")。 //2,(g.lastIndexOf()==g.lastdexof()-2 System.out.println(g.endsWith("字符"));//返回值是bool值. System.out.println(g.lastIndexOf("串")==g.length()-2); //截取子字符串 .substring();开始位置,到结束,包含开始索引位置的字符。 System.out.println(g.substring(1));//只传递开始位置,到结束 //不包含结束索引位置的字符。end-star=截取的字符数 //结束位置索引<=字符串长度。 System.out.println(g.subSequence(3, 5)); //去前后空格 a=" 张 三 "; System.out.println(a.trim()); //去全部空格 System.out.println(a.replace(" ","")); //查找替换 String g1=new String("字符串字符串字符串"); g1.replace("符串","大" ); System.out.println(g1.replace("符串","大" )); System.out.println(g1.replaceFirst("符串", "符大"));//只改变查找的第一个 //replacefirst,replaceall……正则表达式 //replaceall(正则表达式,替换的内容); //大小写转换 g="abcDEfg"; System.out.println(g.toUpperCase());//原先大写不变 System.out.println(g.toLowerCase()); //字符串分割 a="姓名~22~男~地址"; String[]b1=a.split("~"); int i=1; for(String t:b1) { System.out.println(i+" "+t); i++; }
相关文章推荐
- Java字符串String相关2
- AngularJS 事件
- Android 高仿 频道管理----网易、今日头条、腾讯视频 (能够拖动的GridView)附源代码DEMO
- Collection和Collections的区别
- <float.h> 浮点限制 、<limits.h>整数限制
- Ext清空panel中元素的问题
- 自定义View(二)
- unity中的几个坑
- 线性回归
- solr4.9.0本地数据提交异常分析
- 解决docker镜像无法下载的问题
- Android回调机制
- 下拉搜索 bootstrap combox 搜索建议插件
- Java中final、finally、finalize的区别
- 改变UILable里面文字的大小和颜色
- POJ 2492 A Bug's Life【二分图染色法】
- opencv error assertion failed,imread读到的总是空数据,Microsoft C++ 异常: 内存位置 0x0043f888 处的 cv::Exception。
- XStream支持CDATA标签
- Android:interpolator用法
- dd if 创建文件系统