呉ユーキション - 学習の自然生まれのJAVA開発:Stringクラス

パブリック クラスStringDemo {
    公共 静的 ボイドメイン(文字列引数[]){
       CHAR [] helloArray = {R ''、 'U'、 'N'、 'O'、 'O'、 'B' }。
      文字列helloString = 新しい文字列(helloArray)。  
      System.out.println(helloString)。
   } 
}
パブリック クラスStringDemo {
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列サイト = "www.runoob.com" ;
         int型 LEN = site.length(); 
        のSystem.out.println( "チュートリアル初心者URLの長さ:" + LEN ); 
   } 
}
パブリック クラスStringDemo {
     公共 静的 ボイドメイン(文字列引数[]){      
        文字列の文字列1 = "チュートリアル初心者URL:" ;      
        のSystem.out.println( "1" +文字列1 + "www.runoob.com" );   
    } 
}
System.out.printf( "変数値フロート" + 
                  "%F、整数変数の値が" + 
                  "の文字列変数の値の%のD、" + 
                  "%sである"、floatVar、IntVar、 STRINGVAR);
FS文字列; 
FS = String.Formatの( "変数値フロート" + 
                   "%F、整数変数の値が" + 
                   "の文字列変数の値の%のD、" + 
                   "%S"、floatVar、 intVar、STRINGVAR)。
パブリック クラスのテスト{ 
 
    公共 静的 ボイドメイン(文字列引数[]){ 
        文字列S =「www.runoob.com」チャー結果= s.charAt(8 )。
        System.out.println(結果)。
    } 
}
パブリック クラスのテスト{ 
 
    公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 =「文字列」
        文字列STR2 = "文字列" ; 
        文字列STR3 = "Strings123" 
 
        int型の結果= str1.compareTo(STR2)。
        System.out.println(結果)。
      
        結果 = str2.compareTo(STR3)。
        System.out.println(結果)。
     
        結果 = str3.compareTo(0009)。
        System.out.println(結果)。
    } 
}

パブリック クラスのテスト{ 

    公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 =「STRINGS」
        文字列STR2 = "文字列" ; 
        文字列STR3 = "Strings123" 

        int型の結果= str1.compareToIgnoreCase(STR2)。
        System.out.println(結果)。
      
        結果 = str2.compareToIgnoreCase(STR3)。
        System.out.println(結果)。
     
        結果 = str3.compareToIgnoreCase(0009)。
        System.out.println(結果)。
    } 
}

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列S =「菜鸟教程:」
        S = s.concat( "www.runoob.com" )。
        System.out.println(S); 
    } 
}
パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 =「文字列1」
        文字列STR2 =「文字列2」
        StringBufferのSTR3 = 新しい StringBufferの( "文字列1" ); 

        ブール   結果= str1.contentEquals(STR3)。
        System.out.println(結果)。
          
        結果 = str2.contentEquals(STR3)。
        System.out.println(結果)。
    } 
}

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){
         CHAR []技研= { 'H'、 'E'、 'L'、 'L'、 'O'、 ''、 'R&LT'、「U '' N- '' O '' O '' B ' }; 
        で文字列STR2 = "" ; 

        でSTR2 = Str2.copyValueOf(技研)
        のSystem.out.println( "リターン結果:" + でSTR2); 

        STR2で Str2.copyValueOf =(技研、2 ,. 6 
        のSystem.out.println( "リターン結果:" + でSTR2); 
    } 
}
パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列strが = 新しい新規の文字列( "初心者チュートリアル:www.runoob.com" );
         ブールRETVAL; 
 
        RETVAL = Str.endsWith( "runoob" 
        のSystem.out .println( "戻り値=" + RETVAL); 
 
        RETVAL = Str.endsWith( "COM" 
        のSystem.out.println( "戻り値=" + RETVAL); 
    }

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 = 新しい文字列(「runoob」)。
        文字列STR2 = STR1。
        文字列STR3 = 新しい文字列( "runoob" );
        ブールRETVAL; 

        RETVAL = Str1.equals(STR2)。
        System.out.println( "返回值=" + RETVAL)。

        RETVAL = Str1.equals(STR3)。
        System.out.println( "返回值=" + RETVAL)。
    } 
}
パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 = 新しい文字列(「runoob」)。
        文字列STR2 = STR1。
        文字列STR3 = 新しい文字列( "runoob" ); 
        文字列STR4 = 新しい文字列( "RUNOOB" );
        ブールRETVAL; 

        RETVAL = Str1.equals(STR2)。
        System.out.println( "返回值=" + RETVAL)。

        RETVAL =  Str3.equals(STR4)。
        のSystem.out.println( "戻り値=" + RETVAL); 

        RETVAL = Str1.equalsIgnoreCase(STR4)
        のSystem.out.println( "戻り値=" + RETVAL); 
    } 
}
輸入 java.io. * ; 
 
パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列STR1 = 新しい文字列(「runoob」)。
 
        試す{
             バイト [] STR2 = Str1.getBytes()。
            System.out.println( "返回值:" + STR2)。
            
            STR2 = Str1.getBytes( "UTF-8" )。
            System.out.println( "返回值:" + STR2)。
            
            STR2 = Str1.getBytes( "ISO-8859-1" ); 
            System.out.println( "返回值:" +STR2); 
        } キャッチ(にUnsupportedEncodingException E){ 
            するSystem.out.println( "サポートされていない文字セット" ); 
        } 
    } 
}

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列技研は = 新しい新しい文字列( "www.runoob.com" );
         char型の[] = STR2を新しい新しい チャー [6 ]; 

        試み{ 
            Str1.getChars( 4、10。 、0、でSTR2 ); 
            System.out.print(の "コピー列" ); 
            のSystem.out.println)における(STR2は; 
        } キャッチ(例外EX){ 
            するSystem.out.println(「例外がトリガされ... " ); 
        } 
    } 
}

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列引数[]){ 
        文字列strが = 新しい新しい文字列(「www.runoob.com」); 
        のSystem.out.printlnは(「ハッシュコード列である:」+ Str.hashCode ()); 
    } 
}

public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}

public class Test {
    public static void main(String args[]) {
        String Str = new String("菜鸟教程:www.runoob.com");
        String SubStr1 = new String("runoob");
        String SubStr2 = new String("com");
 
        System.out.print("查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o' ));
        System.out.print("从第14个位置查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o', 14 ));
        System.out.print("子字符串 SubStr1 第一次出现的位置:" );
        System.out.println( Str.indexOf( SubStr1 ));
        System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :" );
        System.out.println( Str.indexOf( SubStr1, 15 ));
        System.out.print("子字符串 SubStr2 第一次出现的位置 :" );
        System.out.println(Str.indexOf( SubStr2 ));
    }
}

 

おすすめ

転載: www.cnblogs.com/tszr/p/10960245.html