字符串(String)
Posted skygrass0531
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串(String)相关的知识,希望对你有一定的参考价值。
(1)删除字符串中的字符
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过字符串函数 substring() 函数来删除字符串中的一个字符,我们将功能封装在 removeCharAt 函数中 5 */ 6 public class DeleteStr { 7 public static void main(String[] args){ 8 test1(); 9 test2(); 10 } 11 //方法一 12 private static void test1(){ 13 String startStr = "there is java"; 14 String endStr = startStr.substring(0, 3) + startStr.substring(4); 15 System.out.println("This is test1 :"); 16 System.out.println(endStr); 17 } 18 //方法二 19 private static void test2(){ 20 String str = "there is java"; 21 System.out.println("This is test2 :"); 22 System.out.println(removeStr(str, 3)); 23 } 24 public static String removeStr(String str, int pos){ 25 return str.substring(0, pos) + str.substring(pos + 1); 26 } 27 }
(2)查找子字符串出现的位置
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过字符串函数 strOrig.lastIndexOf(StringName) 来查找子字符串 StringName 在 strOrig 出现的位置 5 */ 6 7 public class LastIndexOf { 8 public static void main(String[] args){ 9 String strOrig = "Hello World, Hello Java"; 10 String strIndex = "Hello"; 11 int lastIndex = strOrig.lastIndexOf(strIndex); 12 if (lastIndex == -1) System.out.println(strIndex + " Not Found"); 13 else System.out.println("Last occurrence of " + strIndex + " is at index " 14 + lastIndex); 15 } 16 }
(3)连接字符串
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过 "+" 操作符和StringBuffer.append() 方法来连接字符串,并比较其性能 5 */ 6 public class StrAdd { 7 public static void main(String[] args){ 8 long startTime1 = System.currentTimeMillis(); 9 for(int i = 0; i < 5000; i++){ 10 String result = "This" + "Is" + "Testing" + "The" + 11 "Different" + "Between" + "String" + "And" + "StringBuffer"; 12 } 13 long endTime1 = System.currentTimeMillis(); 14 System.out.println("使用 + 操作符:" + (endTime1 - startTime1) + "ms"); 15 16 long startTime2 = System.currentTimeMillis(); 17 for(int i = 0; i < 5000; i++){ 18 StringBuffer result = new StringBuffer(); 19 result.append("This"); 20 result.append("Is"); 21 result.append("Testing"); 22 result.append("The"); 23 result.append("Difference"); 24 result.append("Between"); 25 result.append("String"); 26 result.append("And"); 27 result.append("StringBuffer"); 28 } 29 long endTime2 = System.currentTimeMillis(); 30 System.out.println("使用StringBuffer.append()连接:" + (endTime2 - startTime2) + "ms"); 31 } 32 33 }
(4)优化字符串
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过 String.intern() 方法来优化字符串 5 */ 6 public class StrBecomeBest { 7 public static void main(String[] args){ 8 String[] variables = new String[50000]; 9 long startTime0 = System.currentTimeMillis(); 10 for (int i = 0; i < 50000; i++){ 11 variables[i] = "s" + i; 12 } 13 long endTime0 = System.currentTimeMillis(); 14 System.out.println("Creation time of String literals :" + (endTime0 - startTime0) 15 + "ms"); 16 17 long startTime1 = System.currentTimeMillis(); 18 for (int i = 0; i < 50000; i++){ 19 variables[i] = new String("hello"); 20 } 21 long endTime1 = System.currentTimeMillis(); 22 System.out.println("Creation time of String objects with ‘new‘ key word :" + 23 (endTime1 - startTime1) + "ms"); 24 25 long startTime2 = System.currentTimeMillis(); 26 for (int i = 0; i < 50000; i++){ 27 variables[i] = new String("hello"); 28 variables[i] = variables[i].intern(); 29 } 30 long endTime2 = System.currentTimeMillis(); 31 System.out.println("Creation time of String objects with intern() :" + 32 (endTime2 - startTime2) + "ms"); 33 } 34 }
(5)格式化字符串
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 import java.util.Locale; 4 5 /** 6 * 通过 format() 方法来格式化字符串,还可以指定地区来格式化 7 */ 8 public class StrFormat { 9 public static void main(String[] args){ 10 double e = Math.E; //自然对数e 11 System.out.format("%f%n", e); 12 System.out.format(Locale.GERMANY, "%-10.4f%n%n", e); 13 } 14 }
(6)通过两种方式创建字符串
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过两种方式创建字符串,并测试其性能 5 */ 6 public class StrFunctionCompare { 7 public static void main(String[] args){ 8 long startTime1 = System.currentTimeMillis(); 9 for(int i = 0; i < 50000; i++) { 10 String s1 = "hello world"; 11 String s2 = "hello java"; 12 } 13 long endTime1 = System.currentTimeMillis(); 14 System.out.println("通过String关键字创建字符串耗时 " + (endTime1 - startTime1) + "毫秒"); 15 16 long startTime2 = System.currentTimeMillis(); 17 for(int i = 0; i < 50000; i++){ 18 String s3 = new String("hello java"); 19 String s4 = new String("hello world"); 20 } 21 long endTime2 = System.currentTimeMillis(); 22 System.out.println("通过String对象创建创建字符串耗时 " + (endTime2 - startTime2) + "毫秒"); 23 } 24 }
(7)测试两个字符串区域是否相等
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用 regionMatches() 方法测试两个字符串区域是否相等 5 */ 6 public class StrIfSame { 7 public static void main(String[] args){ 8 String str1 = "Welcome to Microsoft"; 9 String str2 = "I work with microsoft"; 10 boolean match1 = str1.regionMatches(true, 11, str2, 12, 9); 11 boolean match2 = str1.regionMatches(false, 11, str2, 12, 9); 12 System.out.println("区分大小写:" + match2); 13 System.out.println("不区分大小写:" + match1); 14 } 15 } 16 17 18 /* 19 * str1.regionMatches(11, str2, 12, 9) 20 * 表示将 str1 字符串从第11个字符"M"开始和 str2 字符串的第12个字符"m"开始逐个比较,共比较 9 对字符 21 * 如果设置第一个参数为 true ,则表示忽略大小写区别,反之则区分大小写 22 * 23 * */
(8)比较字符串并返回ASCII码差值
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 compareTo(object string) 5 * 来比较两个字符串,并返回字符串中第一个字母ASCII的差值。 6 */ 7 public class StringCompare { 8 public static void main(String[] args){ 9 String str = "Hello World"; 10 String anotherString = "hello world"; 11 Object objStr = str; 12 13 System.out.println(str.compareTo(anotherString)); // -32 14 System.out.println(str.compareToIgnoreCase(anotherString)); // 0 15 System.out.println(str.compareTo(objStr.toString())); // 0 16 17 System.out.println("He".compareToIgnoreCase("hD")); // 1 18 System.out.println("Hed".compareToIgnoreCase("hEq")); // -13 19 } 20 } 21 22 23 /* 24 * 总结: 25 * A and B both string 26 * 27 * 1 比较两个字符串首字母的ASCII码差值 28 * A.compareTo(B) 29 * 2 忽略大小写并比较两个字符串首字母的ASCII码差值,若两字母顺位字母相同,则比较下一位,直至相同位置出现不同字母 30 * A.compareToIgnoreCase(B) 31 * 32 * */
(9)字符串替换
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用 java String 类的 replace 方法来替换字符串中的字符 5 */ 6 public class StrReplace { 7 public static void main(String[] args){ 8 String str = "Hello World, Hello Java, Hello !"; 9 System.out.println(str.replace(‘H‘, ‘W‘)); //替换所有 10 System.out.println(str.replaceFirst("He", "WW")); //替换首个 11 System.out.println(str.replaceAll("He", "HH")); //替换所有 12 } 13 }
(10)字符串反转
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用 Java 的反转函数 reverse() 将字符串反转 5 */ 6 7 public class StrReverse { 8 public static void main(String[] args){ 9 String startStr = "this is java!"; 10 String endStr = new StringBuffer(startStr).reverse().toString(); 11 System.out.println("String before reverse :" + startStr); 12 System.out.println("String after reverse :" + endStr); 13 } 14 }
(11)查找子字符串出现位置
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置, 5 * 如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1 6 */ 7 public class StrSearch { 8 public static void main(String[] args){ 9 String str = "There is no people"; 10 String indexStr = "no"; 11 int indexNumber = str.indexOf(indexStr); 12 if(indexNumber != -1) 13 System.out.println("Found ‘" + indexStr + "‘ At Index ‘" + indexNumber); 14 else System.out.println( "‘" + indexStr + "‘ Is Not Found In String ‘" + str + "‘"); 15 } 16 }
(12)字符串分割
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用 split(string) 方法通过指定分隔符将字符串分割为数组 5 */ 6 7 public class StrSplit { 8 public static void main(String[] args){ 9 String str = "www.w3cSchool.com"; 10 String[] temp1, temp2; 11 String splitStr = "."; //需要转义字符进行转义 12 temp1 =str.split(splitStr); 13 System.out.println("----- for 方式 -----"); 14 for (int i = 0; i < temp1.length; i++){ 15 System.out.println(temp1[i]); 16 } 17 System.out.println("----- foreach 方式 -----"); 18 temp2 = str.split(splitStr); 19 for (String x:temp2) 20 System.out.println(x); 21 22 23 24 25 } 26 }
(13)字符串大小写转换
1 package JavaEE.JavaBaseExampleTest.Str; 2 3 /** 4 * 使用 String toUpperCase() 方法将字符串从小写转为大写 5 * 使用 String toLowerCase() 方法将字符串从大写转为小写 6 */ 7 public class UpToLow { 8 public static void main(String[] args){ 9 String strSmall = "strIng apPle tO be Upper"; 10 String strBig = "THis iS UppER"; 11 String upperStr = strSmall.toUpperCase(); 12 String lowerStr = strBig.toLowerCase(); 13 System.out.println("‘" + strSmall + "‘ Upper Is ‘" + upperStr); 14 System.out.println("‘" + strBig + "‘ Lower Is ‘" + lowerStr); 15 16 } 17 }
以上是关于字符串(String)的主要内容,如果未能解决你的问题,请参考以下文章