zhaozhi3758 发表于 2013-2-7 20:31:38

java字符串操作

package com.basic;import java.io.UnsupportedEncodingException;/** * @des java字符串基本操作 * @author zhaozhi3758 * */public class StringT {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}public void simpleT(){System.out.println("\""+str+"\".charAt(int index):"+str.charAt(0));System.out.println("\""+str+"\".endsWith(String str):"+str.endsWith("SD"));System.out.println("\""+str+"\".equals(String str):"+str.equals(""));System.out.println("\""+str+"\".startsWith(String str):"+str.startsWith("b"));System.out.println("\""+str+"\".startsWith(String str,int index):"+str.startsWith("",3));System.out.println("\""+str+"\".toLowerCase():"+str.toLowerCase());System.out.println("\""+str+"\".toUpperCase():"+str.toUpperCase());System.out.println("\""+str+"\".trim():"+str.trim());//忽略前导空白和尾部空白System.out.println("\""+str+"\".replace(char oldChar, char newChar):"+str.replace('s', 'p'));System.out.println("\""+str+"\".replace(String oldstr,String newstr):"+str.replace("s", "-pp-"));}public void indexOfT() {System.out.println("\""+str+"\".indexOf(String str):"+str.indexOf("ds"));//find nothing return -1System.out.println("\""+str+"\".indexOf(String str):"+str.indexOf("a",3));//从第三位索引,包括第三位System.out.println("\""+str+"\".lastIndexOf(String str):"+str.lastIndexOf("a"));//反向搜索System.out.println("\""+str+"\".lastIndexOf(String str):"+str.lastIndexOf("a",12));//反向搜索[索引数字还是从前计数,可设置无限大的值]}public void regexT(){//支持正则System.out.println("\""+str+"\".matches(regex):"+str.matches(".*\\d.*"));//.任意字符String s = "GET/index.html HTTP/1.1";System.out.print("\""+str+"\".split(regex):");String ss[] = s.split("/");for(String Str: ss)System.out.print(Str+" ");System.out.print("\n");System.out.println(str+"\".replaceAll(regex, replacement):"+str.replaceAll("\\w", ""));}public void substringT(){System.out.println("\""+str+"\".substring(int index):"+str.substring(1));System.out.println("\""+str+"\".substring(int beginIndex, int endIndex):"+str.substring(1,4));}public static void main(String[] args){StringT st=new StringT();st.setStr("adscWQQ34546add<s.SDSD");st.simpleT();System.out.println("----------------------------------");st.indexOfT();System.out.println("----------------------------------");st.regexT();System.out.println("----------------------------------");st.substringT();System.out.println("----------------------------------");try {String s=new String("我是大帅哥".getBytes(),"gbk");//将编码换为gbkSystem.out.println(new String(s.getBytes()));} catch (UnsupportedEncodingException e) { e.printStackTrace();}}}
页: [1]
查看完整版本: java字符串操作