JAvaにおける文字列の操作

17147 ワード

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、文字列の1文字を削除する
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、2つの文字列領域が等しいかどうかをテストする
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対の文字を1つずつ比較し始め、文字列が大文字と小文字を区別するため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");
    }
}