java实例
字符串比较
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()) );
}
}

查找字符串最后一次出现的位置
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);
}
}
}

删除字符串中的一个字符
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);
}
}

字符串替换
public class StringReplaceEmp {//字符串替换
public static void main(String[] args) {
String str = "Hello World";
System.out.println( str.replace('H', 'W'));
System.out.println( str.replace("He", "Wa"));
System.out.println( str.replaceAll("He", "Ha"));
}
}

字符串反转
public class StringReverseExample {//字符串反转
public static void main(String[] args) {
String string = "runoob";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("字符串反转前:"+string);
System.out.println("字符串反转后:"+reverse);
}
}

字符串搜索
public class SearchStringEmp {//字符串搜索
public static void main(String[] args) {
String strOrig = "Google Runoob Taobao";
int intIndex = strOrig.indexOf("Runoob");
if(intIndex == -1) {
System.out.println("没有找到字符串 Runoob");
}else {
System.out.println("Runoob 字符串位置 "+ intIndex);
}
}
}

//字符串分割
public class JavaStringSplitEmp {//字符串分割
public static void main(String[] args) {
String str = "www-runoob-com";
String[] temp;
String delimeter = "-";//制定分割字符
temp = str.split(delimeter);//分割字符串
for(int i=0;i<temp.length;i++) {
System.out.println(temp[i]);
System.out.println("");
}
System.out.println("------java for each循环输出的方法-----");
String str1 = "www.runoob.com";
String[] temp1;
String delimeter1 = "\\.";//指定分割字符, .号需要转义
temp1 = str1.split(delimeter1);//分割字符串
for(String x:temp1) {
System.out.println(x);
System.out.println("");
}
}
}

//字符串分隔(StringTokenizer)
import java.util.StringTokenizer;
public class Mainn {//字符串分隔(StringTokenizer)
public static void main(String[] args) {
String str = "This is String , split by StringTokenizer , created by runoob";
StringTokenizer st = new StringTokenizer(str);
System.out.println("----- 通过空格分离 ------");
while(st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("----- 通过逗号分隔 ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while(st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}

字符串小写转大写
public class StringToUpperCaseEmp {//字符串小写转大写
public static void main(String[] args) {
String str = "string runoob";
String strUpper = str.toUpperCase();
System.out.println("原始字符串:" + str);
System.out.println("转换为大写:" + strUpper);
}
}

测试两个字符串区域是否相等
public class StringRegionMatch {//测试两个字符串区域是否相等
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);
}
}

字符串性能比较测试
public class StringComparePerformance {//字符串性能比较测试
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("通过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) + "毫秒");
}
}

字符串优化
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");
}
}

字符串格式化
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);
}
}

连接字符串
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("between");
result.append("String");
result.append("and");
result.append("StringBuffer");
}
long endTime1 = System.currentTimeMillis();
System.out.println("字符串连接" + " - 使用 StringBuffer : " + (endTime1 - startTime1) + "ms");
}
}


浙公网安备 33010602011771号