Java 比较两个按位置移动的字符串

Java 比较两个按位置移动的字符串,java,string,string-matching,Java,String,String Matching,我有两条线: String s1 = "hello"; String s2 ="_hello"; 我希望在比较这些字符串时,为这些字符串返回true(第二个字符串移动了一个位置) 我应该使用哪种String操作方法?有人能给我一个提示吗?您可以使用String类的contains() String s1 = "hello"; String s2 ="_hello"; if(s2.contains(s1)) //true 您还可以使用subString() 是的,您也可以使用inde

我有两条线:

String s1 = "hello"; 
String s2 ="_hello";
我希望在比较这些字符串时,为这些字符串返回
true
(第二个字符串移动了一个位置)

我应该使用哪种
String
操作方法?有人能给我一个提示吗?

您可以使用String类的
contains()

String s1 = "hello";
String s2 ="_hello";

if(s2.contains(s1))
   //true
您还可以使用
subString()

是的,您也可以使用
indexOf()

您可以使用String类的
contains()

String s1 = "hello";
String s2 ="_hello";

if(s2.contains(s1))
   //true
您还可以使用
subString()

是的,您也可以使用
indexOf()


我还没有为android编写程序,所以请对此持保留态度,但请看一下String.indexOf()。我相信Android是基于Java的,所以这应该会有所帮助。

我还没有为Android编写程序,所以对此有点怀疑,但请看一下String.indexOf()。我相信Android是基于Java的,所以这应该会有所帮助。

您应该使用indexOf方法:

int index=s2.indexOf(s1);

if(index ==-1) // s1 is not present into s2.
else index是s2中匹配s1的第一个字符的索引。例如,在下列情况下:

String s1 = "hello"; String s2 ="_hello";

索引将为1。

您应该使用indexOf方法:

int index=s2.indexOf(s1);

if(index ==-1) // s1 is not present into s2.
else index是s2中匹配s1的第一个字符的索引。例如,在下列情况下:

String s1 = "hello"; String s2 ="_hello";

索引将是1。

这两个字符串都是任何结果?为什么不在
s2.equals('''+s1)
?不要在问号之前使用任何标点符号。这两个字符串都是任何结果?为什么不只是
s2.equals('+s1)
?不要在问号之前使用任何标点符号。还可以考虑围绕indexOf()构建一个测试如果它是偏移量这一事实很重要。如果它是偏移量这一事实很重要,还可以考虑围绕indexOf()构建一个测试。