简体   繁体   English

如何检查一个字符串是否包含一系列相等的符号,这些符号与同一字符串中的另一个符号序列相等? (爪哇)

[英]How to check if a string contains a sequence of equal symbols, which are are equal to another sequence of symbols in the same string? (JAVA)

For example I have String例如我有字符串

Cash$$$$$$Ca$$$$$$sh

With left side: Cash$$$$$$ and right side: Ca$$$$$$sh左侧: Cash$$$$$$ ,右侧: Ca$$$$$$sh

I want to implement a method which returns true if the left side contains sequence, of equal characters, which is equal to the sequence in the right side.我想实现一个方法,如果左侧包含相等字符的序列,则该方法返回 true,该序列等于右侧的序列。 Their values must be the same and so as their length.它们的值必须相同,长度也必须相同。 This example returns true.此示例返回 true。

It would be best to write a method.最好写一个方法。

      System.out.println(checkString("Cash$$$$$$Ca$$$$$$sh")); //false
      System.out.println(checkString("Cash$$$$$$Cash$$$$$$")); //true
      System.out.println(checkString("abcdabc")); // false
      System.out.println(checkString("abcabc")); // true;

   public static boolean checkString(String str) {
      // odd length of strings can't have equals halves.
      if (str.length() % 2 == 1) {
         return false;
      }
      int mid = str.length()/2;
      for (char c : str.substring(0,mid).toCharArray()) {
         if (c != str.charAt(mid++)) {
            return false;
         }
      }
      return true;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM