Java独学-数値と文字列操作文字列

5087 ワード

Java一般的な文字列メソッド
例1:文字の取得
charAt(int index)指定された位置の文字を取得する
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "  ,      8    ,          ";
         
        char c = sentence.charAt(0);
         
        System.out.println(c);
           
    }
}

例2:対応する文字配列の取得
toCharArray()対応する文字配列を取得
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "  ,      8    ,         ";
 
        char[] cs = sentence.toCharArray(); //         
         
        System.out.println(sentence.length() == cs.length);
         
    }
}

例3:サブストリングの切り取り
サブストリング切り取りサブストリング
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "  ,      8    ,          ";
         
        //    3        ( 0)
        String subString1 = sentence.substring(3);
         
        System.out.println(subString1);
         
        //    3        ( 0)
        // 5-1       
        //    
        String subString2 = sentence.substring(3,5);
         
        System.out.println(subString2);
         
    }
}

例4:分割
splitは区切り記号によって区切ります
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "  ,      8    ,          ";
         
        //  ,    ,  3     
        String subSentences[] = sentence.split(",");
        for (String sub : subSentences) {
            System.out.println(sub);
        }
           
    }
}

例5:先頭と末尾のスペースを削除
trim先頭と末尾のスペースを削除
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "          ,      8    ,                ";
         
        System.out.println(sentence);
        //      
        System.out.println(sentence.trim());
    }
}

例6:大文字と小文字
toLowerCaseはすべて小文字になりますtoUpperCaseはすべて大文字になります
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "Garen";
         
        //      
        System.out.println(sentence.toLowerCase());
        //      
        System.out.println(sentence.toUpperCase());
         
    }
}

例7:配置
indexOfは、文字またはサブ文字列が現れる位置containsがサブ文字列を含むか否かを判断する
package character;
     
public class TestString {
     
    public static void main(String[] args) {
    
        String sentence = "  ,      8    ,         ";
  
        System.out.println(sentence.indexOf('8')); //          
          
        System.out.println(sentence.indexOf("  ")); //           
          
        System.out.println(sentence.lastIndexOf(" ")); //          
          
        System.out.println(sentence.indexOf(',',5)); //   5  ,      ,   
          
        System.out.println(sentence.contains("  ")); //       "  "
          
    }
}

例8:置換
replaceAll置換すべてのreplaceFirst置換最初の1つのみ
package character;
    
public class TestString {
    
    public static void main(String[] args) {
   
        String sentence = "  ,      8    ,         ";
 
        String temp = sentence.replaceAll("  ", "   "); //     
         
        temp = temp.replaceAll("  ", "  ");
         
        System.out.println(temp);
         
        temp = sentence.replaceFirst(",","");//      
         
        System.out.println(temp);
         
    }
}

練習:大文字小文字の間隔
lengendaryを間隔大文字小文字モード、すなわちLeNgEnDaRyに変更
答え:
package character;
 
public class TestString {
      
    public static void main(String[] args) {
//        lengendary           ,  LeNgEnDaRy 
        String s = "lengendary";
        char[] cs =s.toCharArray();
        int count= 0;
        for (int i = 0; i < cs.length; i++) {
            if(0==i%2)
                cs[i] = Character.toUpperCase(cs[i]);
        }
        String result = new String(cs);
        System.out.printf(result);
 
    }
}

練習:単語の頭文字を大文字にする
Nature has given us that two ears,two eyes,and but one tongue,to the end that we should hear and see more than we speak最後のtwo単語の頭文字を大文字に
答え:
package character;
 
public class TestString {
 
    public static void main(String[] args) {
        //      two       
        String s = "Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
        int index = s.lastIndexOf(" two ");
         
        char[] cs = s.toCharArray();
        cs[index +1] = Character.toUpperCase(cs[index+1]);
        String result = new String(cs);
        System.out.printf(result);
 
    }
}