文字列が別の文字列の部分文字列であるかどうかをチェック

ACZ:

チェックは、文字列が別の部分文字列であるについて私は素敵な運動との記事を読みました。

演習の内容は次のとおりです。

コマンドラインから2つの文字列パラメータを取るプログラムを書きます。2番目の文字列が最初の文字列の部分文字列は、(あなたがサブストリングまたは正規表現ライブラリを含む任意の他の標準関数、SUBSTRを使用することはできません)であれば、プログラムは確認する必要があります。

第二サブ手段における*の各存在は、それが最初の文字列のゼロ個以上の文字に一致させることができます。

例を考えてみましょう:入力文字列1:ABCD入力文字列2:プログラムC *は、文字列2を文字列1のサブであることを評価する必要があります。

それはバックスラッシュ(\)が付いている場合さらに、アスタリスク(*)は、通常の文字として考えることができます。バックスラッシュ(\)は、アスタリスク(*)の前以外のすべてのケースでは、通常の文字とみなされます。

私は、まず2番目の文字列が長い最初の(しかし、(「AB」、「* B」)でのテストは、これは正しいテストですが、方法が失敗した一つの問題がある)以上であることを確認した、シンプルなアプリを書きました:

public static boolean checkCharactersCount(String firstString, String secondString) {
    return (firstString.length() > 0 && secondString.length() > 0) &&
            (firstString.length() > secondString.length());

...と、次の検証subtringです。

public static boolean checkSubstring(String firstString, String secondString) {
    int correctCharCounter = 0;
    int lastCorrectCharAtIndex = -1;

    for (int i = 0; i < secondString.length(); i++) {
        for (int j = 0; j < firstString.length(); j++) {
            if (j > lastCorrectCharAtIndex) {

                if ((secondString.charAt(i) == firstString.charAt(j)) || secondString.charAt(i) == '*') {
                    correctCharCounter++;
                    lastCorrectCharAtIndex = j;
                }

                if (correctCharCounter >= secondString.length())
                    return true;
            }
        }
    }

    return false;
}

しかし、2つの問題があります。

  1. 上記の文字の継続性をサポートしていないとして、私のコード(例えば、テストのために:checkSubstring(「abacd」、「BCD」)がtrueを返すが、それは間違っている - falseを返す必要があります!)
  2. 任意のアイデアはどのように「\ *」などの特殊記号を確認するには?テスト(checkSubstring( "へのサンプルBC"、 "\ B")

どのように解決のあなたのビジョンはありますか?:)

アビシェークOZA:

これを試してみてください:(コメントは説明のために追加されました)

// only for non empty Strings
public boolean isSubString(String string1,String string2)
{
    // step 1: split by *, but not by \*
    List<String>list1 = new ArrayList<String>();
    char[]cs = string2.toCharArray();
    int lastIndex = 0 ;
    char lastChar = 0 ;
    int i = 0 ;
    for(; i < cs.length ; ++i)
    {
        if(cs[i]=='*' && lastChar!='\\')
        {
            list1.add(new String(cs,lastIndex,i-lastIndex).replace("\\*", "*"));
            //earlier buggy line:
            //list1.add(new String(cs,lastIndex,i-lastIndex));
            lastIndex = i + 1 ;
        }
        lastChar = cs[i];
    }
    if(lastIndex < i )
    {
        list1.add(new String(cs,lastIndex,i-lastIndex).replace("\\*", "*"));
    }
    // step 2: check indices of each string in the list
    // Note: all indices should be in proper order.
    lastIndex = 0;
    for(String str : list1)
    {
        int newIndex = string1.indexOf(str,lastIndex);
        if(newIndex < 0)
        {
            return false;
        }
        lastIndex = newIndex+str.length();
    }
    return true;
}

場合にはあなたが使用することを許可されていないString.indexOf()その関数を記述public int indexOf(String string1,String string2, int index2)し、この文を置き換えます

int newIndex = string1.indexOf(str,lastInxdex);

この文で:

int newIndex = indexOf(string1, str,lastInxdex);

================================================== ======

付録A:私がテストコード:

package jdk.conf;

import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public static void main(String[] args)
    {
        Test01 test01 = new Test01();
        System.out.println(test01.isSubString("abcd", "a*c"));
        System.out.println(test01.isSubString("abcd", "bcd"));
        System.out.println(test01.isSubString("abcd", "*b"));
        System.out.println(test01.isSubString("abcd", "ac"));
        System.out.println(test01.isSubString("abcd", "bd"));
        System.out.println(test01.isSubString("abcd", "b*d"));
        System.out.println(test01.isSubString("abcd", "b\\*d"));
        System.out.println(test01.isSubString("abcd", "\\*d"));
        System.out.println(test01.isSubString("abcd", "b\\*"));

        System.out.println(test01.isSubString("a*cd", "\\*b"));
        System.out.println(test01.isSubString("", "b\\*"));
        System.out.println(test01.isSubString("abcd", ""));

        System.out.println(test01.isSubString("a*bd", "\\*b"));
    }
    // only for non empty Strings
    public boolean isSubString(String string1,String string2)
    {
        // step 1: split by *, but not by \*
        List<String>list1 = new ArrayList<String>();
        char[]cs = string2.toCharArray();
        int lastIndex = 0 ;
        char lastChar = 0 ;
        int i = 0 ;
        for(; i < cs.length ; ++i)
        {
            if(cs[i]=='*' && lastChar!='\\')
            {
                list1.add(new String(cs,lastIndex,i-lastIndex).replace("\\*", "*"));
                lastIndex = i + 1 ;
            }
            lastChar = cs[i];
        }
        if(lastIndex < i )
        {
            list1.add(new String(cs,lastIndex,i-lastIndex).replace("\\*", "*"));
        }
        // step 2: check indices of each string in the list
        // Note: all indices should be in proper order.
        lastIndex = 0;
        for(String str : list1)
        {
            int newIndex = string1.indexOf(str,lastIndex);
            if(newIndex < 0)
            {
                return false;
            }
            lastIndex = newIndex+str.length();
        }
        return true;
    }
}

出力:

true
true
true
false
false
true
false
false
false
false
false
true
true

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=173558&siteId=1