You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测字符串是否包含另一字符串的部分内容(非完整匹配)

How to Detect if One String Contains Partial Content from Another

Hey there! Let's break down how to solve this based on your scenario: you've got two strings:

  1. John has 2 apples.
  2. Cody plays xbox in John's basement.

You know they share the substring John, but right now you only know how to check for exact whole-word matches using str1.toLowerCase().contains(str2.toLowerCase())—and you want to figure out how to detect if the first string contains any partial content from the second, not just full words.

First, a quick clarification: the contains() method you're already using doesn't actually restrict itself to whole words—it checks for any matching substring, regardless of word boundaries. The catch here is that you're probably using it with full words right now, but what you need is a way to dynamically find shared substrings between the two strings, not just check for a specific hardcoded term.

Here are a few practical approaches tailored to your needs:

1. Brute-force Substring Check (Simple & Effective for Most Cases)

If you want to check for any shared substring of a minimum length (to avoid trivial matches like single spaces or characters), you can iterate through all possible substrings of the second string and check if the first string contains them.

For example, in Java:

public static boolean hasCommonSubstring(String str1, String str2, int minLength) {
    // Normalize to lowercase to make the check case-insensitive
    String lowerStr1 = str1.toLowerCase();
    String lowerStr2 = str2.toLowerCase();
    
    // Iterate through every possible starting index in str2 for substrings of minLength
    for (int i = 0; i <= lowerStr2.length() - minLength; i++) {
        String substring = lowerStr2.substring(i, i + minLength);
        if (lowerStr1.contains(substring)) {
            return true;
        }
    }
    return false;
}

To use this with your example, call hasCommonSubstring(str1, str2, 4)—since "john" is 4 characters long, this will return true immediately when it finds the matching substring.

2. Partial Word Match (Ignoring Punctuation)

If you specifically care about matching parts of words (like "John" vs "John's"), split both strings into words (stripping out punctuation) and check if any word from the first string contains a substring of a word from the second, or vice versa.

Example code:

public static boolean hasPartialWordMatch(String str1, String str2) {
    // Split strings into words (split on non-word characters like apostrophes, spaces)
    String[] words1 = str1.toLowerCase().split("\\W+");
    String[] words2 = str2.toLowerCase().split("\\W+");
    
    for (String word1 : words1) {
        for (String word2 : words2) {
            // Check if either word contains the other as a substring
            if (word1.contains(word2) || word2.contains(word1)) {
                return true;
            }
        }
    }
    return false;
}

This will catch that "john" from your first string is a substring of "john's" from the second string.

3. Quick Win: Use contains() for Known Substrings

If you already know the specific substring you're looking for (like "John"), your existing method works perfectly—you don't need to limit it to whole words. Just call:

str1.toLowerCase().contains("john")

This will return true right away, since "John" exists in the first string.

Key Takeaways

  • Use the brute-force substring check if you need to dynamically find any shared partial content between the two strings.
  • Use the partial word match if you want to focus on word fragments (ignoring punctuation).
  • Stick with your existing contains() method if you're checking for a specific known substring.

内容的提问来源于stack exchange,提问作者user9598926

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:52:46