String类的常用方法总结
2011-01-13 00:30
363 查看
近来面试较多这这种题,以前太注重ssh了,现在还是要回到基础
package com.yangfan.string; class StringDemo{ //取出字符串中指定位置的字符 public void charAt(){ String str = "hello"; char c =str.charAt(3); System.out.println(c); } //由字符串--->字符数组 public void charArray(){ 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,6); } //字节数组--->字符串 public void ByteArray(){ String str = "hello world"; byte b[] = str.getBytes(); String str1 = new String(b);//变回字符串 } //替换内容 public void replaceContext(){ String str = "hello world"; String newStr = str.replaceAll("l", "x"); } //字符串截取 public void substring(){ String str = "hello world"; String sub1 = str.substring(6); String sub2 = str.substring(0, 3); } //字符串拆分 public void split(String regex){ String str ="hello world"; String s []=str.split(regex);//按regex条件拆分 for(String st:s){ System.out.println(s); } } //字符串查找(直接查找) public void contains(String s){ String str ="hello world"; System.out.println(str.concat("hello"));//结果是true } public void indexOf(){ String str ="hello world"; if(str.indexOf("hello")!=-1){ System.out.println("查找到要的内容!"); } } /* * 去掉左右空格:public String trim(); * 取得字符串长度:public int length(); * 转大写:public String toUpperCase(); * 转小写:public String toLowerCase(); * */ //按要求拆分:TOM:89|JERRY:90|TONY:79 //拆成:TOM ->89 JERRY ->90 TONY -> 79 public void splitDemo(){ String str = "TOM:89|JERRY:90|TONY:79"; String[] str2 =str.split("//|"); for(String st:str2){ String[] s2=st.split(":"); System.out.println(s2[0]+"-->"+s2[1]); } } }
相关文章推荐
- Java-String类的常用方法总结
- String类常用方法总结
- JavaSE002_String类总结之常用方法及实例
- String类常用方法总结
- Java-String类的常用方法总结
- String类的常用方法总结
- Java-String类的常用方法总结
- java学习总结(16.07.14)String类及其的常用方法
- 黑马程序员——String类中常用方法总结
- JavaSE001_String类总结之构造器和常用方法
- java中String类常用方法总结
- java中String类常用方法总结
- Java-String类的常用方法总结
- String类的常用方法总结
- String类中常用方法总结
- Java-String类的常用方法总结
- Java String类的常用方法总结
- String类一些比较常用的方法总结
- Java-String类的常用方法总结
- String类的常用方法总结