【文字列の取得方法】

パッケージ cn.itcast.day08.demo02;

/*
String 内の関連情報を取得するために一般的に使用されるメソッドは次のとおりです。

public int length(): 文字列に含まれる文字数と文字列の長さを取得します。
public String concat(String str): 現在の文字列とパラメータ文字を連結して、戻り値の新しい文字列を作成します。
public char charAt(int index): 指定されたインデックス位置にある単一の文字を取得します。(インデックスは0から始まります)
public int indexOf(String str): この文字列内でパラメータ文字列が初めて出現するインデックス位置を検索します。そうでない場合は、-1 の値を返します。
 */
パブリック クラス Demo02StringGet {
    public static void main(String[] args) {
        // 文字列の長さを取得する
        int length = "djksfhkjsdhfkjsdh".length();
        System.out.println("文字列の長さは次のとおりです: "+length);

        // 文字列を結合する
        文字列 str1 = "こんにちは";
        文字列 str2 = "ワールド";
        文字列 str3 = str1.concat(str2);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("======================);

        //指定されたインデックス位置にある単一の文字を取得します
        char ch = "Hello".charAt(1);
        System.out.println("インデックス 1 の文字は次のとおりです: "+ch);
        System.out.println("===========================);

        //元の文字列内でパラメータ文字列が出現する最初のインデックス位置を検索します
        //何もない場合は、-1 の値を返します
        文字列元 = "HelloWorld";
        int インデックス = オリジナル.indexOf("llo");
        System.out.println("最初のインデックス値は次のとおりです: "+index);

        System.out.println("HelloWorld".indexOf("abc"));
    }
}

おすすめ

転載: blog.csdn.net/m0_48114733/article/details/123297833