目次
int CompareToIgnoreCase(String str)
int IndexOf(int ch, int fromIndex):
int IndexOf(String str, int fromIndex):
int lastIndexOf(int ch, int fromIndex):
int lastIndexOf(String str, int fromIndex):
文字列 toUpperCase();文字列 toLowerCase():
String replaceAll(文字列正規表現、文字列置換):
String replaceFirst(文字列正規表現、文字列置換)
文字列部分文字列(int beginIndex, int endIndex):
文字列比較方法:
ブール値等しい(オブジェクト anObject):
2 つの文字列が等しいかどうかを比較し、等しい場合は true を返し、そうでない場合は false を返します。
public static void main(String[] args) {
String a = "asdf";
System.out.println(a.equals("aaa"));
System.out.println(a.equals("asdf"));
}
int CompareTo(String s):
2 つの文字列が等しいかどうかを比較するには、まず辞書順に比較します。等しくない文字が現れた場合は、 2 つの文字間のサイズの差を直接返します。最初の k 文字が等しい場合 (k は 2 文字の最小長です)、2 つの文字列の長さの差を返します。
public static void main(String[] args) {
String a = "asdf";
System.out.println(a.compareTo("aaa"));
System.out.println(a.compareTo("asdf"));
System.out.println(a.compareTo("asd"));
}
int CompareToIgnoreCase(String str)
比較では文字の大文字と小文字は無視されます。戻り値の規則は次のとおりです。
- まず、辞書の順序に従ってサイズを比較し、等しくない文字がある場合は、 2 つの文字のサイズの差を直接返します。
- 最初の k 文字が等しい場合 (k は 2 文字の最小長)、2 つの文字列の長さの差を返します。
public static void main(String[] args) {
String a = "asdf";
System.out.println(a.compareToIgnoreCase("aaa"));
System.out.println(a.compareToIgnoreCase("ASDF"));
System.out.println(a.compareToIgnoreCase("asd"));
}
文字列検索方法:
char charAt(int インデックス):
インデックス位置 の文字を返します。インデックスが負であるか範囲外の場合、IndexOutOfBoundsException 例外がスローされます。
public static void main(String[] args) {
String a = "asdf";
System.out.println(a.charAt(0));
System.out.println(a.charAt(3));
System.out.println(a.charAt(5));
}
int IndexOf(int ch):
ch が最初に出現する位置を返します。そうでない場合は、-1 を返します。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.indexOf('d'));
System.out.println(a.indexOf('a'));
System.out.println(a.indexOf('h'));
}
int IndexOf(int ch, int fromIndex):
fromIndex 位置から始まる ch を検索し、 -1 を返さずに最初に出現した位置を返します。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.indexOf('d', 3));
System.out.println(a.indexOf('a', 1));
System.out.println(a.indexOf('h',0));
}
intindexOf(String str):
-1 を返さずに、 str が最初に出現する位置を返します。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.indexOf("dd"));
System.out.println(a.indexOf("ss"));
}
int IndexOf(String str, int fromIndex):
-1 を返さずに、 fromIndex 位置から始まる str の最初の出現を検索します。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.indexOf("dd", 3));
System.out.println(a.indexOf("ss", 0));
}
int lastIndexOf(int ch):
後ろから前に検索し、最初に ch が出現する位置に戻り、そうでない場合は -1 を返します。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.lastIndexOf('d'));
System.out.println(a.lastIndexOf('s'));
System.out.println(a.lastIndexOf('v'));
}
int lastIndexOf(int ch, int fromIndex):
fromIndex の位置から検索を開始し、ch が最初に出現する位置を後ろから前に検索します。-1 は返されません。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.lastIndexOf('d', 2));
System.out.println(a.lastIndexOf('d', 3));
System.out.println(a.lastIndexOf('d', 4));
System.out.println(a.lastIndexOf('g', 5));
}
int lastIndexOf(String str):
後ろから前に検索し、 str が最初に出現する位置を返します。-1 は返されません。
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.lastIndexOf("dd"));
System.out.println(a.lastIndexOf("as"));
System.out.println(a.lastIndexOf("bv"));
}
int lastIndexOf(String str, int fromIndex):
back からfront に最初に str が現れる位置を検索します。この位置の添字がfromIndexより大きくない場合は戻り、それ以外の場合は前方へ検索を続けます。どれも -1 を返しません
public static void main(String[] args) {
String a = "asdddf";
System.out.println(a.lastIndexOf("dd", 3));
System.out.println(a.lastIndexOf("dd", 2));
System.out.println(a.lastIndexOf("dd", 1));
System.out.println(a.lastIndexOf("as", 0));
System.out.println(a.lastIndexOf("bv", 0));
}
文字列変換
String.valueOf():
すべての基本型の値を文字列型に変換します
public static void main(String[] args) {
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf('a');
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
文字列 toUpperCase();
文字列 toLowerCase():
元の文字列を大文字に変換した新しい文字列を返します。
元の文字列を小文字に変換した新しい文字列を返します。
public static void main(String[] args) {
String s1 = "heLLo";
String s2 = "HEllO";
System.out.println(s1.toUpperCase());
System.out.println(s2.toLowerCase());
}
char[] toCharArray();
文字列(文字値[]):
文字列を配列に変換します。元の文字列は影響を受けません
配列を文字列に変換します。元の配列は影響を受けません
public static void main(String[] args) {
String s = "hello";
char[] ch = s.toCharArray();
System.out.println(Arrays.toString(ch));
String s2 = new String(ch);
System.out.println(s2);
}
文字列の置換方法:
String replaceAll(文字列正規表現、文字列置換):
指定されたすべてのコンテンツを置き換えます
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "O"));
}
String replaceFirst(文字列正規表現、文字列置換)
最初のコンテンツを置き換える
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.replaceFirst("l", "O"));
}
String[] 分割(文字列正規表現):
すべての文字列を分割する
public static void main(String[] args) {
String str = "hello world hello" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
}
文字列部分文字列(int beginIndex, int endIndex):
[beginIndex,endIndex) の範囲内の文字列をインターセプトします
public static void main(String[] args) {
String str = "helloworld" ;
System.out.println(str.substring(0, 5));
}