Java 实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java 实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7832字,纯文字阅读大概需要12分钟。
内容图文
Java 字符串
字符串比较
以下实例中我们通过字符串函数 compareTo (string) ,compareToIgnoreCase(String) 及 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())); } }字符串比较
以上代码实例输出结果为:
result查找字符串最后一次出现的位置
以下实例中我们通过字符串函数 strOrig.lastIndexOf(Stringname) 来查找子字符串 Stringname 在 strOrig 出现的位置:
实例代码如下:
public class SearchlastString { public static void main(String[] args) { String strOrig = "Hello world ,Hello Runoob"; int lastIndex = strOrig.lastIndexOf("Runoob"); if(lastIndex == - 1){ System.out.println("没有找到字符串 Runoob"); }else{ System.out.println("Runoob 字符串最后出现的位置: "+ lastIndex); } } }lastIndexOf函数
以上代码实例输出结果为:
Runoob 字符串最后出现的位置: 19
删除字符串中的一个字符
以下实例中我们通过字符串函数 substring() 函数来删除字符串中的一个字符,我们将功能封装在 removeCharAt 函数中。
实例代码如下:
package com.feng; public class Test { public static void main(String[] args) { String str = "My name is Mr.Feng"; System.out.println("删除后的数据为:"+removeStr(str,3)); } public static String removeStr(String str,int pos){ return str.substring(0,pos)+str.substring(pos+1); } }substring 应用
以上代码实例输出结果为:
删除后的数据为:My ame is Mr.Feng
字符串替换
如何使用java替换字符串中的字符呢?
以下实例中我们使用 java String 类的 replace 方法来替换字符串中的字符:
package com.feng; public class Test { public static void main(String[] args) { String str = "The god is a girl"; System.out.println(str.replace("T","F")); System.out.println(str.replaceFirst("The", "That")); System.out.println(str.replaceAll("is", "are")); } }replace 函数
以上代码实例输出结果为:
Fhe god is a girl That god is a girl The god are a girlView Code
字符串反转
以下实例演示了如何使用 Java 的反转函数 reverse() 将字符串反转:
package com.feng; public class Test { public static void main(String[] args) { String str = "book"; String reverse = new StringBuffer(str).reverse().toString(); System.out.println("反转前:"+str); System.out.println("反转后"+reverse); } }StringBuffer
以上代码实例输出结果为:
反转前:book 反转后koobView Code
字符串搜索
以下实例使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如果存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:
package com.feng; public class Test { public static void main(String[] args) { String str = "My name is Angel"; int index = str.indexOf("name"); if(index==-1){ System.out.println("单词不存在"); }else{ System.out.println("单词位置为"+index); } } }indexOf
以上代码实例输出结果为:
单词位置为3View Code
字符串分割
以下实例使用了 split(string) 方法通过指定分隔符将字符串分割为数组:
package com.feng; public class Test { public static void main(String[] args) { String str1 = "www-cnblogs-Mr-Feng"; String sp = "-"; String[] temp = str1.split(sp); for(int i=0;i<temp.length;i++){ System.out.println(temp[i]); } System.out.println("---------------------------------------------------------"); String str2 = "www.cnblogs.Mr.Feng"; String sp2 = "\\."; String [] temp2 = str2.split(sp2); for(String x:temp2){ System.out.println(x); } } }split
以上代码实例输出结果为:
www cnblogs Mr Feng --------------------------------------------------------- www cnblogs Mr FengView Code
字符串小写转大写
以下实例使用了 String toUpperCase() 方法将字符串从小写转为大写:
package com.feng; public class Test { public static void main(String[] args) { String str1 = "www,cnblogs,Mr.Feng"; String str2 = str1.toUpperCase(); System.out.println(str2); } }toUpperCase()
测试两个字符串区域是否相等
以下实例使用了 regionMatches() 方法测试两个字符串区域是否相等:
package com.feng; public class Test { public static void main(String[] args) { String first_str = "Welcome to Microsoft"; String second_str = "I work with microsoft"; boolean match1 = first_str. regionMatches(11, second_str, 12, 9); boolean match2 = first_str. regionMatches(true, 11, second_str, 12, 9); //第一个参数 true 表示忽略大小写区别 System.out.println("区分大小写返回值:" + match1); System.out.println("不区分大小写返回值:" + match2); } }regionMatches()
first_str.regionMatches(11, second_str, 12, 9) 表示将 first_str 字符串从第11个字符"M"开始和 second_str 字符串的第12个字符"M"开始逐个比较,共比较 9 对字符,由于字符串区分大小写,所以结果为false。
如果设置第一个参数为 true ,则表示忽略大小写区别,所以返回 true。
以上代码实例输出结果为:
区分大小写返回值:false 不区分大小写返回值:trueView Code
字符串性能比较测试
以下实例演示了通过两种方式创建字符串,并测试其性能:
package com.feng; public class Test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s1 = "hello"; String s2 = "hello"; } long endTime = System.currentTimeMillis(); System.out.println(endTime-startTime); System.out.println("-------------------------------------------"); long startTime1 = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s1 = new String("hello"); String s2 = new String("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println(endTime1-startTime1); } }View Code
以上代码实例输出结果为:
3 ------------------------------------------- 8View Code
字符串优化
以下实例演示了通过 String.intern() 方法来优化字符串:
public class StringOptimization { public static void main(String[] args){ 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 startTime1 = System.currentTimeMillis(); for(int i=0;i<50000;i++){ variables[i] = new String("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println("使用 new 关键字:" + (endTime1 - startTime1) + " ms"); long startTime2 = System.currentTimeMillis(); for(int i=0;i<50000;i++){ variables[i] = new String("hello"); variables[i] = variables[i].intern(); } long endTime2 = System.currentTimeMillis(); System.out.println("使用字符串对象的 intern() 方法: " + (endTime2 - startTime2) + " ms"); } }View Code
以上代码实例输出结果为:
直接使用字符串: 3 ms 使用 new 关键字:5 ms 使用字符串对象的 intern() 方法: 10 msView Code
字符串格式化
以下实例演示了通过 format() 方法来格式化字符串,还可以指定地区来格式化:
import java.util.*; public class StringFormat { public static void main(String[] args){ double e = Math.E; System.out.format("%f%n", e); System.out.format(Locale.CHINA , "%-10.4f%n%n", e); //指定本地为中国(CHINA) } }View Code
以上代码实例输出结果为:
2.718282 2.7183View Code
连接字符串
"+" 操作符和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("字符串连接" + " - 使用 + 操作符 : " + (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("字符串连接" + " - 使用 StringBuffer : " + (endTime1 - startTime1)+ " ms"); } }View Code
以上代码实例输出结果为:
字符串连接 - 使用 + 操作符 : 0 ms 字符串连接 - 使用 StringBuffer : 6 msView Code
内容总结
以上是互联网集市为您收集整理的Java 实例全部内容,希望文章能够帮你解决Java 实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。