String类常用属性方法小结
2016-08-20 11:18
225 查看
1. 字符串替换
replaceAll(String old,String new)//将字符串中的old字符,替换为new字符。
例如:
最后b的输出结果为“hella warld”
2. 判断是否以指定的字符串开头或结尾
Str.startsWith(String start)
Str.endsWith(String end)
例如:
3. 大小写转换
①小写变大写:str.toUpperCase();
②大写变小写:str.toLowerCase();
4. 拆分字符串
Str.split(“**”)
例如:
最终st[]的输出结果为:h,e,l,l,o,
5. 字符串截取
① 从指定位置开始一直截取到字符串的最后:str.substring(int begin)
② 截取指定范围的字符串:str.substring(intbegin,int end)
6. 查找指定的字符串是否存在
①从头开始查找,查到之后返回所在位置:str.indexOf(String s)
②从指定位置开始查找,查到之后返回所在位置:str.indexOf(String s,int begin)
例如:
7. 取一个字符串的长度
Str.length
8. 从字符串中取出指定位置的字符
Str.charAt(int a)
例如:
9. 移除字符串中指定的字符
①字符串的开头:str.TrimStart()
②字符串的末尾:str.TrimEnd()
③字符串的开头和末尾去除空格:str.trim();
例如:
10. 删除指定位置的字符
Str.remove(int begin,int end);
replaceAll(String old,String new)//将字符串中的old字符,替换为new字符。
例如:
<span style="font-size:18px;">String a=”hello world”; String b=a.replaceAll(“o”,”a”);</span>
最后b的输出结果为“hella warld”
2. 判断是否以指定的字符串开头或结尾
Str.startsWith(String start)
Str.endsWith(String end)
例如:
<span style="font-size:18px;">String str1=”aahello world”; String str2=”hello worldaa”; If(str1.startsWith(“aa”)){ System.out.println(str1); }</span>
3. 大小写转换
①小写变大写:str.toUpperCase();
②大写变小写:str.toLowerCase();
4. 拆分字符串
Str.split(“**”)
例如:
<span style="font-size:18px;">String str=”hello”; String st[]=str.split(“,”); For(int i=0;i<st.length;i++){ System.out.println(st[i]); }</span>
最终st[]的输出结果为:h,e,l,l,o,
5. 字符串截取
① 从指定位置开始一直截取到字符串的最后:str.substring(int begin)
② 截取指定范围的字符串:str.substring(intbegin,int end)
6. 查找指定的字符串是否存在
①从头开始查找,查到之后返回所在位置:str.indexOf(String s)
②从指定位置开始查找,查到之后返回所在位置:str.indexOf(String s,int begin)
例如:
<span style="font-size:18px;">String str=”hello”; Int a=str.indexOf(“o”);//从头开始查找字符o,最后a的结果为4. Int b=str.indexOf(“o”,2);//从第2个位置开始查找o,最后a的结果为4.</span>
7. 取一个字符串的长度
Str.length
8. 从字符串中取出指定位置的字符
Str.charAt(int a)
例如:
<span style="font-size:18px;">String str=”hello”; Str.charAt(3);//取出字符串中第4个字符</span>
9. 移除字符串中指定的字符
①字符串的开头:str.TrimStart()
②字符串的末尾:str.TrimEnd()
③字符串的开头和末尾去除空格:str.trim();
例如:
<span style="font-size:18px;">char[] a={“h”,”o”}; String b=”hello”; String c=b.trimstart(a); String d=b.trimend(a); C=”ello“; d=”hell”;</span>
10. 删除指定位置的字符
Str.remove(int begin,int end);
相关文章推荐
- 用 PNChart 做统计图
- 2014-11-12-自定义子弹
- Python读写文件
- 陶瓷贴片电容的容值和封装对应的选择关系
- linux下修改ip地址
- 找零问题
- C的内部函数和外部函数
- 实现两列等宽布局的几种方法
- 关于http长连接使用的分享
- jzoj 1148. 【克罗地亚】INSTRUKCIJE
- POJ 1789 Truck History
- C的内部函数和外部函数
- XUtils===XUtils3框架的基本使用方法(一)
- Modbus通讯资料总结
- 中软Java学习8月10日笔记
- 第一次机房收费系统——饮血
- dubbo学习笔记 -- 安装(一)
- HDU 5862 Counting Intersections
- spring+jersery集成注意问题
- 基础5