检查两个字符串的内容

huangapple go评论107阅读模式
英文:

Checking contents of 2 strings

问题

我刚开始学习Java编程,我想知道如何判断一个特定的字符串,比如 "Hello",是否包含 "Hlo"(按照这个顺序)。我使用了 .contains 方法,但它只在字母连续的情况下起作用。那么如何处理类似上面的情况呢?

附注:例如,如果我们询问是否在 "Hello" 中包含 "oll",会返回 false,因为 "Hello" 不包含 "oll";如果我们询问是否在 "Hello" 中包含 "elo",会返回 true。
谢谢。

英文:

I am new to java programming and I was wondering how I can know if a certain string like "Hello" contains "Hlo" (in this order). I used the .contains method but it only works if the letters are consecutive. So how can I handle cases like the above?

P.S: For example, if we ask if "oll" is in "Hello" false is returned as "Hello" doesn't contain "oll", and if we ask if "elo" is in "Hello", true is returned.
Thank you.

答案1

得分: 1

使用正则表达式,您可以通过在字符之间放置可能数量的字符来构建用于测试的正则表达式,这些字符将被填充到缺失的位置上。

例如,Hlo -> .*H.*l.*l.*o.*

public static boolean checkStrings(String a, String b) {
    String pattern = "^.+" + String.join(".+", b.split("")) + ".+$";
    return a.matches(pattern);
}

public static void main(String[] args) {
    System.out.println(checkStrings("Hello", "Hllo")); // true
    System.out.println(checkStrings("Hello", "oll"));  // false
}
-----
这可以内联化

```java
public static boolean checkStrings(String a, String b) {
    return a.matches(".*" + String.join(".*", b.split("")) + ".*");
}
英文:

Using regex, you could build a regex from the word to test, by putting, between character a possible amount of chars, which will be filled with the missing ones

For example Hlo -> .*H.*l.*l.*o.*

public static boolean checkStrings(String a, String b) {
    String pattern = "^.*" + String.join(".*", b.split("")) + ".*$";
    return a.matches(pattern);
}

public static void main(String[] args) {
    System.out.println(checkStrings("Hello", "Hllo")); // true
    System.out.println(checkStrings("Hello", "oll"));  // false
}

This can be inlined

public static boolean checkStrings(String a, String b) {
    return a.matches(".*" + String.join(".*", b.split("")) + ".*");
}

答案2

得分: 1

以下是翻译好的部分:

这被称为子序列。您可以迭代主字符串,如果找到匹配项,则增加搜索字符串的索引,最后如果主字符串中的所有搜索字符串字符都匹配,则它是一个子序列。

boolean isSubSequence(String searchString, String mainString) {
    int j = 0;
    for (int i = 0; i < mainString.length() && j < searchString.length(); i++) {
        if (searchString.charAt(j) == mainString.charAt(i)) {
            j++;
        }
    }
    return (j == searchString.length());
}
英文:

That's call subsequence. You can iterate over main string and if match found increase the index of search string and at last if all character of search string match in main string then it's a subsequence.

  boolean isSubSequence(String searchString, String mainString) {
    int j = 0;
    for (int i = 0; i &lt; mainString.length() &amp;&amp; j &lt; searchString.length(); i++)
      if (searchString.charAt(j) == mainString.charAt(i))
        j++;
    return (j == searchString.length());
  }

答案3

得分: 1

    public static boolean containsCharsInOrder(String str, String pattern) {
        int j = 0;
    
        for (int i = 0; i < str.length() && j < pattern.length(); i++)
            if (str.charAt(i) == pattern.charAt(j))
                j++;
    
        return j == pattern.length();
    }
英文:

Just, be simple!

public static boolean containsCharsInOrder(String str, String pattern) {
    int j = 0;

    for (int i = 0; i &lt; str.length() &amp;&amp; j &lt; pattern.length(); i++)
        if (str.charAt(i) == pattern.charAt(j))
            j++;

    return j == pattern.length();
}

答案4

得分: 0

你可以迭代遍历字符串的每个字符,如果找到第一个正确的字母,然后检查其他字符是否紧随其后。

一个可能的实现如下:

public static boolean containsString(String base, String search) {
    for (int i = 0; i <= (base.length() - search.length()); i++) {
        if (base.charAt(i) == search.charAt(0)) {
            boolean containsString = true;
            for (int j = 1; j < search.length(); j++) {
                if (search.charAt(j) != base.charAt(i + j)) {
                    containsString = false;
                }
            }
            if (containsString)
                return true;
        }
    }
    return false;
}
英文:

You could iterate over each character of the String and if the first correct letter is found, then check if the other characters are following.

A possible implementation of this would be following:

public static boolean containsString(String base, String search) {
    for(int i = 0; i &lt;= (base.length() - search.length()); i ++) {
        if(base.charAt(i) == search.charAt(0)) {
            boolean containsString = true;
            for(int j = 1; j &lt; search.length(); j ++) {
                if(search.charAt(j) != base.charAt(i+j)) {
                    containsString = false;
                }
            }
            if(containsString)
                return true;
        }
    }
    return false;
}

答案5

得分: 0

我会将“search”字符串分解为字符,并针对每个字符检查其在字符串中的 indexOf,从先前找到的索引开始:

public static boolean containsCharsInOrder(String str, String chars) {
    int len = chars.length();
    int startIndex = 0;
    for (int i = 0; i < len; ++i) {
        char ch = chars.charAt(i);
        int ind = str.indexOf(ch, startIndex);
        if (ind == -1) {
            return false;
        }
        startIndex = ind;
    }
    return true;
}
英文:

I'd break the "search" string into characters, and for each such character check its indexOf in the string, starting from the previously found index:

public static boolean containsCharsInOrder(String str, String chars) {
    int len = chars.length();
    int startIndex = 0;
    for (int i = 0; i &lt; len; ++i) {
        ch = chars.charAt(i);
        int ind = str.indexOf(ch, startIndex);
        if (ind == -1) {
            return false;
        }
        startIndex = ind;
    }
    return true;
}

huangapple
  • 本文由 发表于 2020年9月9日 03:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63800193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定