java中String类的常用方法
2015-10-21 20:25
405 查看
字符串与字符之间的转换
public String(char[ ] value) 将全部的字符数组内容转换为字符串
public String(char[ ] value,int offset,int count) 将部分数组内容转换为字符串,起点offset,转换个数count
public char charAt(int index) 取得指定索引位置上的字符
public char[ ] toArray() 将字符串转换为字符数组
示例:
字符串与字节之间的转换
public String(byte[ ] bytes) 将全部的字节数组内容转换为字符串
public String(byte[ ] bytes,int offset,int length) 将部分字节数组内容转换为字符串,起点offset,转换个数length
public byte[ ] getbytes()
public byte[ ] getbytes(String charsetName) 将字符串变为字节数组
throws UnsupportedEncodingException 字符串转码操作
示例:
字符串比较
public boolean equals(String anObject) 区分大小写的相等判断
public boolean equalsIgnoreCase
(String anotherString) 不区分大小写的相等判断
public int compareTo(String anotherString) 比较两个字符串的大小(>返回正值 <返回负值==返回0)
示例:
字符串查找
public boolean contains(String s) 查找指定的字符串是否存在
public int indexOf(string str) 从头查找指定字符串的位置,找不到返回-1
public int indexOf(string str,int fromIndex) 由指定位置向后查找字符串位置
public int lastIndexOf(String str) 从末尾向前查找字符串位置,找不到返回-1
public int lastIndexOf(String str,int fromIndex) 由指定位置向前查找字符串位置
public boolean startsWith(String prefix) 判断是否以指定的字符串开头
public boolean startsWith(String prefix,int toffset) 接上一个
public boolean endsWith(String suffix) 判断是否以指定的字符串结尾
示例:
字符串替换
public String replaceAll(String regex,
String replacement) 全部替换
public String replaceFirst(String regex,
String replacement) 替换首个
注意:regex是需要被替换的,replacement是代替者
示例:
字符串截取
Public String substring(int beginIndex) 指定位置截取
Public String substring(int beginIndex,int endIndex) 截取指定范围
示例:
字符串拆分
Public String[ ] split(String regex) 按照指定的字符串全拆分
Public String[ ] split(String regex,int limit) 拆分为指定的个数
示例:
有些时候对于拆分字符regex需要做转义处理才能正常拆分,详见我的博文java中的正则表达式
除了以上核心方法外,还有几个常用的方法
Public int length() 获取字符串长度
public String toLowerCase() ——>小写
public String toUpperCase() ——>大写
public String trim() 去掉字符串左右空格
示例:
public String(char[ ] value) 将全部的字符数组内容转换为字符串
public String(char[ ] value,int offset,int count) 将部分数组内容转换为字符串,起点offset,转换个数count
public char charAt(int index) 取得指定索引位置上的字符
public char[ ] toArray() 将字符串转换为字符数组
示例:
public class StringAPIDemo02{ public static void main(String args[]){ String str = "hello world !!!@" ; char c[] = str.toCharArray() ; // 将字符串变为字符数组 for(int i=0;i<c.length;i++){ System.out.print(c[i] + "、"); } String str1 = new String(c) ; // 将全部的字符数组重新变为String String str2 = new String(c,0,5) ; // 将0~5的字符数组重新变为String System.out.println("\n"+str1) ; System.out.println(str2) ; } };
字符串与字节之间的转换
public String(byte[ ] bytes) 将全部的字节数组内容转换为字符串
public String(byte[ ] bytes,int offset,int length) 将部分字节数组内容转换为字符串,起点offset,转换个数length
public byte[ ] getbytes()
public byte[ ] getbytes(String charsetName) 将字符串变为字节数组
throws UnsupportedEncodingException 字符串转码操作
示例:
public class StringAPIDemo03{ public static void main(String args[]){ String str = "hello world !!!@" ; byte b[] = str.getBytes() ; // 将字符串变为byte数组 String str1 = new String(b) ; // 将全部的字符数组重新变为String String str2 = new String(b,0,5) ; // 将0~5的字符数组重新变为String System.out.println(str1) ; System.out.println(str2) ; } };
字符串比较
public boolean equals(String anObject) 区分大小写的相等判断
public boolean equalsIgnoreCase
(String anotherString) 不区分大小写的相等判断
public int compareTo(String anotherString) 比较两个字符串的大小(>返回正值 <返回负值==返回0)
示例:
public class StringAPIDemo09{ public static void main(String args[]){ String str = "hello world"; System.out.println(str.equals("Hello world")) ; System.out.println(str.equalsIgnoreCase("Hello world")) ; } };
字符串查找
public boolean contains(String s) 查找指定的字符串是否存在
public int indexOf(string str) 从头查找指定字符串的位置,找不到返回-1
public int indexOf(string str,int fromIndex) 由指定位置向后查找字符串位置
public int lastIndexOf(String str) 从末尾向前查找字符串位置,找不到返回-1
public int lastIndexOf(String str,int fromIndex) 由指定位置向前查找字符串位置
public boolean startsWith(String prefix) 判断是否以指定的字符串开头
public boolean startsWith(String prefix,int toffset) 接上一个
public boolean endsWith(String suffix) 判断是否以指定的字符串结尾
示例:
public class StringAPIDemo04{ public static void main(String args[]){ String str = "**hello world ##" ; System.out.println(str.startsWith("**")) ; System.out.println(str.endsWith("##")) ; } };
字符串替换
public String replaceAll(String regex,
String replacement) 全部替换
public String replaceFirst(String regex,
String replacement) 替换首个
注意:regex是需要被替换的,replacement是代替者
示例:
public class StringAPIDemo05{ public static void main(String args[]){ String str = "hello world" ; String newStr = str.replaceAll("l","x") ; System.out.println(newStr) ; } };
字符串截取
Public String substring(int beginIndex) 指定位置截取
Public String substring(int beginIndex,int endIndex) 截取指定范围
示例:
public class StringAPIDemo06{ public static void main(String args[]){ String str = "hello world" ; String sub1 = str.substring(6) ; String sub2 = str.substring(0,5) ; System.out.println(sub1) ; System.out.println(sub2) ; } };
字符串拆分
Public String[ ] split(String regex) 按照指定的字符串全拆分
Public String[ ] split(String regex,int limit) 拆分为指定的个数
示例:
public class StringAPIDemo07{ public static void main(String args[]){ String str = "hello world" ; String s[] = str.split(" ") ; // 按照空格拆分 for(String st:s){ System.out.println(st) ; } } };
有些时候对于拆分字符regex需要做转义处理才能正常拆分,详见我的博文java中的正则表达式
除了以上核心方法外,还有几个常用的方法
Public int length() 获取字符串长度
public String toLowerCase() ——>小写
public String toUpperCase() ——>大写
public String trim() 去掉字符串左右空格
示例:
public class StringAPIDemo11{ public static void main(String args[]){ String str = " hello world " ; System.out.println(str.trim()) ; System.out.println(str.trim().toUpperCase()) ; System.out.println(str.trim().length()) ; } };
相关文章推荐
- JDK目录文件说明
- eclipse ant 打包 apk
- 如何优化Java垃圾回收机制3
- java万年历
- 如何监控Java垃圾回收机制2
- JAVAEclipse使用学习总结
- 关于Struts、Hibernate、Spring的简单理解
- JAVAString类学习总结
- JAVA数据类型学习总结
- Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法
- Java程序员从笨鸟到菜鸟之(八十二)细谈Spring(十一)深入理解spring+struts2整合(附源码)
- Java程序员从笨鸟到菜鸟之(八十一)细谈Spring(十)深入源码分析Spring之HibernateTemplate 和HibernateDaoSupport
- JAVA数组学习总结
- Java程序员从笨鸟到菜鸟之(八十)细谈Spring(九)spring+hibernate声明式事务管理详解
- Java程序员从笨鸟到菜鸟之(七十八)细谈Spring(七)spring之JDBC访问数据库及配置详解
- Java程序员从笨鸟到菜鸟之(七十九)细谈Spring(八)spring+hibernate整合基本详解
- Java编程中“为了性能”需做的26件事
- JAVA流程控制学习总结
- Spring MVC 教程,快速入门,深入分析
- Java中一些常用的排序程序代码