String 类的常用方法

川长思鸟来 2022-04-13 03:49 377阅读 0赞

1,求长判相等:

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. String string =reader .next();
  8. int n=string .length();
  9. System.out.println(n);
  10. String string2 =reader.next();
  11. if(string .equals(string2))
  12. {
  13. System.out.println("YES");
  14. }
  15. else
  16. {
  17. System.out.println("NO");
  18. }
  19. reader.close();
  20. }
  21. }
  22. akkhhhjjkk
  23. 10
  24. akkhhhjjkk
  25. YES

2: 判头判尾是否相同

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. String string =reader .next();
  8. String string2 =reader.next();
  9. if(string .startsWith(string2))
  10. {
  11. System.out.println("YES");
  12. }
  13. else
  14. {
  15. System.out.println("NO");
  16. }
  17. reader.close();
  18. if(string .endsWith(string2))
  19. {
  20. System.out.println("YES");
  21. }
  22. else
  23. {
  24. System.out.println("NO");
  25. }
  26. }
  27. }
  28. aaabbb
  29. aaa
  30. YES

3:判断当前字符串的某个位置间的子字符串和另一个字符串是否相等

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. String string =reader .next();
  8. String string2 =reader.next();
  9. if(string .regionMatches(0, string2, 0, 3))
  10. {
  11. System.out.println("YES");
  12. }
  13. else
  14. {
  15. System.out.println("NO");
  16. }
  17. reader.close();
  18. }
  19. }
  20. eeeccc
  21. eee
  22. YES
  23. import java.util.*;
  24. public class Main
  25. {
  26. public static void main(String[] args)
  27. {
  28. Scanner reader =new Scanner(System.in);
  29. String string =reader .next();
  30. String string2 =reader.next();
  31. if(string .regionMatches(2, string2, 0, 3))
  32. {
  33. System.out.println("YES");
  34. }
  35. else
  36. {
  37. System.out.println("NO");
  38. }
  39. reader.close();
  40. }
  41. }
  42. eeccceee
  43. ccc
  44. YES

该方法的重载方法:(忽略大小写)

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. String string =reader .next();
  8. String string2 =reader.next();
  9. if(string .regionMatches(true,0, string2, 0, 3))
  10. {
  11. System.out.println("YES");
  12. }
  13. else
  14. {
  15. System.out.println("NO");
  16. }
  17. reader.close();
  18. }
  19. }
  20. aaaVVV
  21. AAA
  22. YES

4 判断两个字符串按字典序列比较大小

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .next();
  10. String string2 =reader.next();
  11. if(string .compareTo(string2)==0)
  12. {
  13. System.out.println("YES");
  14. }
  15. if(string .compareTo(string2)>0)
  16. {
  17. System.out.println("NO I LOVE");
  18. }
  19. if(string .compareTo(string2)<0)
  20. {
  21. System.out.println("NO I DONT LOVE");
  22. }
  23. }
  24. reader.close();
  25. }
  26. }
  27. aaaccc
  28. aaaccc
  29. YES
  30. abcde
  31. b
  32. NO I DONT LOVE
  33. abcde
  34. abbde
  35. NO I LOVE

5 判断当前字符串是否包含另一个字符串

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .next();
  10. String string2 =reader.next();
  11. if(string .contains(string2))
  12. {
  13. System.out.println("I LOVE YOU");
  14. }
  15. else
  16. {
  17. System.out.println("I DONT LOVE YOU");
  18. }
  19. }
  20. reader.close();
  21. }
  22. }
  23. aaaaaa
  24. ccc
  25. I DONT LOVE YOU
  26. aaaa
  27. a
  28. I LOVE YOU

6 从开端和当前位置分别检索有无此字符串:

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .next();
  10. String string2 =reader.next();
  11. int n=string .indexOf(string2);
  12. if(n!=-1)
  13. {
  14. System.out.println(n);
  15. }
  16. else
  17. {
  18. System.out.println("NO");
  19. }
  20. }
  21. reader.close();
  22. }
  23. }
  24. aaaaaa
  25. cccccc
  26. NO
  27. aaaaaa
  28. a
  29. 0
  30. aaaaaa
  31. bbbbaa
  32. NO
  33. abcdabd
  34. abd

从当前位置开始检索:

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .next();
  10. String string2 =reader.next();
  11. int n=string .indexOf(string2,4);
  12. if(n!=-1)
  13. {
  14. System.out.println(n);
  15. }
  16. else
  17. {
  18. System.out.println("NO");
  19. }
  20. }
  21. reader.close();
  22. }
  23. }
  24. ILOVEYOU
  25. YOU
  26. 5
  27. ILOVEYOU
  28. LOVE
  29. NO

7 获取模式串的子串:

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .next();
  10. String string2 =string.substring(1,5);
  11. System.out.println(string2);
  12. }
  13. reader.close();
  14. }
  15. }
  16. ILOVEYOU
  17. LOVE

8将一个字符串去掉空格(仅限前后):

  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner reader =new Scanner(System.in);
  7. while(reader.hasNext())
  8. {
  9. String string =reader .nextLine();
  10. String string2 =string.trim();
  11. System.out.println(string2);
  12. }
  13. reader.close();
  14. }
  15. }
  16. I LOVE YOU
  17. I LOVE YOU
  18. I LOVE YOU (后面有空格)
  19. I LOVE YOU

发表评论

表情:
评论列表 (有 0 条评论,377人围观)

还没有评论,来说两句吧...

相关阅读