//字符串搜索最后出现的位置 这个例子显示了使用strOrig.lastIndexOf(Stringname) 方法如何确定一个字符串里面的子字符串的最后一个位置。 public class SearchlastString { public static void main(String[] args) { String strOrig="Hello world,Hello Reader"; int lastIndex=strOrig.lastIndexOf("Hello"); if(lastIndex==-1) { System.out.println("Hello not found!"); } else System.out.println("Last occurence of Hello is at index"+lastIndex); } } //字符串比较 下面的示例比较两个字符串用 str.compareTo (string) , str.compareToIgnoreCase(String) 和 str.compareTo(object string),并返回字符串比较的第一个奇字的ascii不同。 public class StringCompareEmp{ public static void main(String args[]){ String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr.toString())); } } //字符串删除 下面的例子演示了使用removeCharAt(string,position) 方法如何从一个字符串特定位置删除一个字符。 java中substring的用法 str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str; str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str; public class Main { public static void main(String args[]) { String str = "this is Java"; System.out.println(removeCharAt(str, 3)); } public static String removeCharAt(String s, int pos) { return s.substring(0, pos) + s.substring(pos + 1); } } //字符串分隔替换 这个例子说明了如何使用Java String类的replace 方法替换字符或子字符串。 public class StringReplaceEmp{ public static void main(String args[]){ String str="Hello World"; System.out.println( str.replace( 'H','W' ) ); System.out.println( str.replaceFirst("He", "Wa") ); System.out.println( str.replaceAll("He", "Ha") ); } } //字符串反转 下面的示例显示了如何通过命令行参数反转字符串。该程序使用StringBuffer(String string)方法输入字符串并反转缓冲区,然后将缓冲区使用toString() 转换成String。 public class StringReverseExample{ public static void main(String[] args){ String string="abcdef"; String reverse = new StringBuffer(string). reverse().toString(); System.out.println(" String before reverse: "+string); System.out.println("String after reverse: "+reverse); } } //字符串搜索 这个例子显示如何利用indexOf()方法在一个String对象中搜索一个词,如果找到那么返回字符串中一个字的位置索引。否则返回-1。 public class SearchStringEmp{ public static void main(String[] args) { String strOrig = "Hello readers"; int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } } } //字符串分割 下面的例子将一个字符串转换成若干子字符串,在str的split(string)方法的帮助下,然后打印字符串。 public class JavaStringSplitEmp{ public static void main(String args[]){ String str = "jan-feb-march"; String[] temp; String delimeter = "-"; temp = str.split(delimeter); for(int i =0; i < temp.length ; i++){ System.out.println(temp[i]); System.out.println(""); str = "jan.feb.march"; delimeter = "\."; temp = str.split(delimeter); } for(int i =0; i < temp.length ; i++){ System.out.println(temp[i]); System.out.println(""); temp = str.split(delimeter,2); for(int j =0; j < temp.length ; j++){ System.out.println(temp[i]); } } } } 结果: jan feb march jan jan jan feb.march feb.march feb.march //字符串转换为大写 下面的示例将一个字符串转换为大写的使用String toUpperCase()方法的情况下完成。 public class StringToUpperCaseEmp { public static void main(String[] args) { String str = "string abc touppercase "; String strUpper = str.toUpperCase(); System.out.println("Original String: " + str); System.out.println("String changed to upper case: " + strUpper); } } //字符串区域匹配 下面的例子使用regionMatches()方法确定两个字符串区域匹配。 public class StringRegionMatch{ public static void main(String[] args){ String first_str = "Welcome to Microsoft"; String second_str = "I work with Microsoft"; boolean match = first_str. regionMatches(11, second_str, 12, 9); System.out.println("first_str[11 -19] == " + "second_str[12 - 21]:-"+ match); } } 结果: first_str[11 -19] == second_str[12 - 21]:-true //字符串连接 下面的例子显示了通过使用“+”操作符和StringBuffer.append()方法拼接的性能。 public class StringConcatenate{ public static void main(String[] args){ long startTime = System.currentTimeMillis(); for(int i=0;i<5000;i++){ String result = "This is" + "testing the" + "difference"+ "between" + "String"+ "and"+ "StringBuffer"; } long endTime = System.currentTimeMillis(); System.out.println("Time taken for string" + "concatenation using + operator : " + (endTime - startTime)+ " ms"); long startTime1 = System.currentTimeMillis(); for(int i=0;i<5000;i++){ StringBuffer result = new StringBuffer(); result.append("This is"); result.append("testing the"); result.append("difference"); result.append("between"); result.append("String"); result.append("and"); result.append("StringBuffer"); } long endTime1 = System.currentTimeMillis(); System.out.println("Time taken for String concatenation" + "using StringBuffer : " + (endTime1 - startTime1)+ " ms"); } } //结果: Time taken for stringconcatenation using + operator : 0 ms Time taken for String concatenationusing StringBuffer : 22 ms //如何缓冲字符串 下面的例子字符串缓冲区,并通过使用emit()方法刷新它。 public class StringBuffer{ public static void main(String[] args) { countTo_N_Improved(); } private final static int MAX_LENGTH=30; private static String buffer = ""; private static void emit(String nextChunk) { if(buffer.length() + nextChunk.length() > MAX_LENGTH) { System.out.println(buffer); buffer = ""; } buffer += nextChunk; } private static final int N=100; private static void countTo_N_Improved() { for (int count=2; count7lt;=N; count=count+2) { emit(" " + count); } } } //运行结果: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82
Java常用的字符串操作
最新推荐文章于 2023-06-23 23:39:26 发布