多语言展示
当前在线:953今日阅读:91今日分享:37

Java 学习之字符串(二)

本次学习内容如下:字符串反转、字符串查找、字符串分割、字符串小写转大写、测试两个字符串区域是否相等、字符串性能比较测试、字符串优化、连接字符串
工具/原料
1

Win7开发环境

2

MyEclipse开发工具

3

jdk1.6

方法/步骤
1

首先需要学习相应函数如下:reverse()  、indexOf() 、 split(string) 、 String toUpperCase()、 regionMatches() 、 String.intern() 、StringBuffer.append()

2

1.使用 Java 的反转函数 reverse()将字符串反转见下图:

3

2.查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:见下图:

4

3.指定分隔爹何符将字符串分割为数组见下图:

5

4.将字符串从小写转为大写见下图:

6

5.测试两个字符串区域是否相等见下图:

7

6.两种方式创建字符串,并测试其性能见下图:

8

7.优化字符串见下图:

9

8.连接字符串,并比较其性能见下图:

10

10.完整代码如下:package baidutest;public class baiduTest { public static void main(String[] args) { String string = 'No man or woman is worth your tears, and the one who is worth make you cry.'; // 1.使用 Java 的反转函数 reverse()将字符串反转 String reverse = new StringBuffer(string).reverse().toString(); System.out.println('原数据:' + string); System.out.println('反向后的数据:' + reverse); // 2.查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1: int intIndex = string.indexOf('tears'); if (intIndex == -1) { System.out.println('查无此词'); } else { System.out.println('此词位置蕉蹲在:' + intIndex); } // 3.指定分隔符将字符串分割为数组 String[] temp; String delimeter = ' '; // 指定分割字符为空格,有些分个符需要转义 temp = string.split(delimeter); // 分割字符串 // 循环遍历 for (int i = 0; i < temp.length; i++) { System.out.println(temp[i]); } // 4.将字符串从小写转为大写 String strCapital = string.toUpperCase(); System.out.println('大写: ' + strCapital); // 5.测试两个字符串区域是否相等 // string.regionMatches(11, string2, 12, 9) // 表示将 string 字符串从第9个字符'w'开始和 string2 字符串的第26个字符'W'开始逐个比较,共比较 // 5对字符斤册泪,由于字符串区分大小写,所以结果为false。 // 如果设置第一个参数为 true ,则表示忽略大小写区别,所以返回 true。 String string2 = ' I'm Not A Girl, Not Yet A Woman '; boolean match1 = string.regionMatches(9, string2, 26, 5); boolean match2 = string.regionMatches(true, 9, string2, 26, 5); System.out.println('区分大小写返回值:' + match1); System.out.println('不区分大小写返回值:' + match2); // 6.两种方式创建字符串,并测试其性能 long startTime = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { String s1 = 'hello'; String s2 = 'hello'; } long endTime = System.currentTimeMillis(); System.out.println('通过 String 关键词创建字符串' + ' : ' + (endTime - startTime) + ' 毫秒'); long startTime1 = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { String s3 = new String('hello'); String s4 = new String('hello'); } long endTime1 = System.currentTimeMillis(); System.out.println('通过 String 对象创建字符串' + ' : ' + (endTime1 - startTime1) + ' 毫秒'); // 7.优化字符串 String variables[] = new String[50000]; for (int i = 0; i < 50000; i++) { variables[i] = 's' + i; } long startTime0 = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { variables[i] = 'hello'; } long endTime0 = System.currentTimeMillis(); System.out.println('字符串的创建时间 : ' + (endTime0 - startTime0) + ' ms'); long startTime2 = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { variables[i] = new String('hello'); } long endTime2 = System.currentTimeMillis(); System.out .println('用new关键字创建字符串对象的时间 : ' + (endTime2 - startTime2) + ' ms'); long startTime3 = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { variables[i] = new String('hello'); variables[i] = variables[i].intern(); } long endTime3 = System.currentTimeMillis(); System.out.println('用new关键字创建字符串对象的时间(有优化后): ' + (endTime3 - startTime3) + ' ms'); //8.连接字符串,并比较其性能 //No man or woman is worth your tears, and the one who is worth make you cry. long startTime4 = System.currentTimeMillis();      for(int i=0;i<5000;i++){         String result = 'No man'         + ' or woman'         + ' is worth'+ 'your tears,'         + 'and'+ ' the one'+ ' who is worth make you cry.';      }      long endTime4 = System.currentTimeMillis();      System.out.println('使用 ‘+’操作符连接:' + (endTime4 - startTime4)+ ' ms');      long startTime5 = System.currentTimeMillis();      for(int i=0;i<5000;i++){         StringBuffer result = new StringBuffer();         result.append('No man');         result.append(' or woman');         result.append(' is worth');         result.append('your tears,');         result.append('and');         result.append(' the one');         result.append(' who is worth make you cry.');      }      long endTime5 = System.currentTimeMillis();      System.out.println('使用 StringBuffer连接:'+ (endTime5 - startTime5)+ ' ms'); }}

注意事项

运行环境不同,产生的结果不同

推荐信息