1,求长判相等:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
String string =reader .next();
int n=string .length();
System.out.println(n);
String string2 =reader.next();
if(string .equals(string2))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
reader.close();
}
}
akkhhhjjkk
10
akkhhhjjkk
YES
2: 判头判尾是否相同
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
String string =reader .next();
String string2 =reader.next();
if(string .startsWith(string2))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
reader.close();
if(string .endsWith(string2))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
aaabbb
aaa
YES
3:判断当前字符串的某个位置间的子字符串和另一个字符串是否相等
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
String string =reader .next();
String string2 =reader.next();
if(string .regionMatches(0, string2, 0, 3))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
reader.close();
}
}
eeeccc
eee
YES
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
String string =reader .next();
String string2 =reader.next();
if(string .regionMatches(2, string2, 0, 3))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
reader.close();
}
}
eeccceee
ccc
YES
该方法的重载方法:(忽略大小写)
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
String string =reader .next();
String string2 =reader.next();
if(string .regionMatches(true,0, string2, 0, 3))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
reader.close();
}
}
aaaVVV
AAA
YES
4 判断两个字符串按字典序列比较大小
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .next();
String string2 =reader.next();
if(string .compareTo(string2)==0)
{
System.out.println("YES");
}
if(string .compareTo(string2)>0)
{
System.out.println("NO I LOVE");
}
if(string .compareTo(string2)<0)
{
System.out.println("NO I DONT LOVE");
}
}
reader.close();
}
}
aaaccc
aaaccc
YES
abcde
b
NO I DONT LOVE
abcde
abbde
NO I LOVE
5 判断当前字符串是否包含另一个字符串
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .next();
String string2 =reader.next();
if(string .contains(string2))
{
System.out.println("I LOVE YOU");
}
else
{
System.out.println("I DONT LOVE YOU");
}
}
reader.close();
}
}
aaaaaa
ccc
I DONT LOVE YOU
aaaa
a
I LOVE YOU
6 从开端和当前位置分别检索有无此字符串:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .next();
String string2 =reader.next();
int n=string .indexOf(string2);
if(n!=-1)
{
System.out.println(n);
}
else
{
System.out.println("NO");
}
}
reader.close();
}
}
aaaaaa
cccccc
NO
aaaaaa
a
0
aaaaaa
bbbbaa
NO
abcdabd
abd
从当前位置开始检索:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .next();
String string2 =reader.next();
int n=string .indexOf(string2,4);
if(n!=-1)
{
System.out.println(n);
}
else
{
System.out.println("NO");
}
}
reader.close();
}
}
ILOVEYOU
YOU
5
ILOVEYOU
LOVE
NO
7 获取模式串的子串:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .next();
String string2 =string.substring(1,5);
System.out.println(string2);
}
reader.close();
}
}
ILOVEYOU
LOVE
8将一个字符串去掉空格(仅限前后):
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
while(reader.hasNext())
{
String string =reader .nextLine();
String string2 =string.trim();
System.out.println(string2);
}
reader.close();
}
}
I LOVE YOU
I LOVE YOU
I LOVE YOU (后面有空格)
I LOVE YOU