我有兩個字符串(str1,str2),用例是這樣的1) 將 str1 中的最后 i 個字符與 str2 中的前 i 個字符進(jìn)行比較2) 將 str1 中的前 i 個字符與 str2 中的最后 i 個字符進(jìn)行比較我知道如何在 C++ 中進(jìn)行如下操作int len1 = str1.length();
int len2 = str2.length();1)str1.compare(len1-i, i, str2, 0, i)2)str1.compare(0, i, str2, len2-i, i)如何使用 regionMatches 方法或其他方法在 java 中實(shí)現(xiàn)相同的功能。想不通。
2 回答

慕勒3428872
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個贊
在 Java 中,你可以通過 regionMatches 來完成:
String str1 = new String("Hello, How are you");
String str2 = new String("How do you do?");
System.out.print("Result of Test1: " );
System.out.println(str1.regionMatches(7, str2, 0, 3));
結(jié)果:
Result of Test1: true
添加回答
舉報
0/150
提交
取消