Java中字符串的操作

1、查找字符串最后一次出现的位置

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);
      }
   }
}

以上代码实例输出结果为:
Runoob 字符串最后出现的位置: 19

2、删除字符串中的一个字符

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);
   }
}

以上代码实例输出结果为:
thi is Java

3、字符串替换

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") );
   }
}

以上代码实例输出结果为:
Wello World
Wallo World
Hallo World

4、字符串反转

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); 
} 
}

以上代码实例输出结果为:
字符串反转前:runoob
字符串反转后:boonur

5、字符串搜索

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);
      }
   }
}

以上代码实例输出结果为:
Runoob 字符串位置 7

**6、字符串分public class JavaStringSplitEmp {

public static void main(String args[]){
  String str = "www-runoob-com";
  String[] temp;
  String delimeter = "-";  // 指定分割字符
  temp = str.split(delimeter); // 分割字符串
  // 普通 for 循环
  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("");
  }
  }
  }
码实例输出结果为:
www

runoob

com

------java for each循环输出的方法-----
www

runoob

com

7、字符串小写转大写

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);
    }
}
以上代码实例输出结果为:
原始字符串: string runoob
转换为大写: STRING RUNOOB

8、测试两个字符串区域是否相等

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);
   }
}

first_str.regionMatches(11, second_str, 12, 9) 表示将 first_str 字符串从第11个字符”M”开始和 second_str 字符串的第12个字符”M”开始逐个比较,共比较 9 对字符,由于字符串区分大小写,所以结果为false。
如果设置第一个参数为 true ,则表示忽略大小写区别,所以返回 true。
以上代码实例输出结果为:
区分大小写返回值:false
不区分大小写返回值:true
9、字符串格式化

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)
    }
}

以上代码实例输出结果为:
2.718282
2.7183

10、连接字符串

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");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值