如果字符串包含 Char[] 数组,则在该字符串中进行搜索的代码?

发布于 10-27 04:13 字数 518 浏览 1 评论 0原文

我正在开发一个应用程序,该应用程序具有以下功能:如果文本 (searchString) 位于 .txt 文件中(.txt 文件也是阿拉伯语言),则可以用阿拉伯语搜索该文本。

由于 Android 不支持 100% 阿拉伯语,因此 String.indexof() 无法正常工作。因此,我想,我会将 searchString 放入 Char[] 数组中,而不是比较整个单词,而是比较每个字符。因此,我将 searchString 放入 char[] 中,并开始将数组与字符串进行比较。

是否可以在任何地方使用搜索 char[] 中的序列的代码数组转换成字符串?

示例:

char[]={t,e,s,t}  String1{qqwtestq} String2{qwqtqwe}  -> String1:TRUE   String2:FALSE

谢谢

I'm developing an app that has a feature to search a text (searchString ) in Arabic language if it is in a .txt file (.txt file is also in Arabic language).

Since Android doesn't support Arabic 100%, String.indexof() doesn't work properly. So, I thought, I would put the searchString into a Char[] array and instead of comparing the whole word, I compare every character. So I put the searchString into a char[] and start comparing the array to the String

Is it available anywhere a code that searches if the sequence that is in the char[] array is into a String?

example:

char[]={t,e,s,t}  String1{qqwtestq} String2{qwqtqwe}  -> String1:TRUE   String2:FALSE

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年2024-11-03 04:13:58

indexOfcontains 不使用任何类型的字符编码,例如,您可以使用字符编码中未使用的字符。即这些函数会忽略它。

所有 String.indexOf() 和 contains 都是比较字符。我不确定您期望 100% 阿拉伯语支持是什么样的行为。这是 indexOf()/contains() 的简化版本 打印

public static int indexOf(String string, char[] chars) {
    LOOP:
    for (int i = 0; i < string.length() - chars.length; i++) {
        for (int j = 0; j < chars.length; j++)
            if (string.charAt(i + j) != chars[j])
                continue LOOP;
        return i;
    }
    return -1;
}

public static void main(String args[]) {
    char[] chars = "test".toCharArray();
    String one = "qqwtestq";
    String two = "qwqtqwe";
    String str = new String(chars);
    System.out.println("indexOf(" + one+", " + Arrays.toString(chars) + ") = " + indexOf(one, chars));
    System.out.println(one + ".indexOf(" + str + ") = " + one.indexOf(str));
    System.out.println("indexOf(" + two+", " + Arrays.toString(chars) + ") = " + indexOf(two, chars));
    System.out.println(two + ".indexOf(" + str + ") = " + two.indexOf(str));

    char[] chars2 = { '\uffff', '\uFeFF' };
    String test = "qqw\uffff\uFeFFq";
    String str2 = new String(chars2);
    System.out.println("indexOf(" + test+", " + Arrays.toString(chars2) + ") = " + indexOf(test, chars2));
    System.out.println(test + ".indexOf(" + str2 + ") = " + test.indexOf(str2));
}

indexOf(qqwtestq, [t, e, s, t]) = 3
qqwtestq.indexOf(test) = 3
indexOf(qwqtqwe, [t, e, s, t]) = -1
qwqtqwe.indexOf(test) = -1
indexOf(qqw??q, [?, ?]) = 3
qqw??q.indexOf(??) = 3

能提供一个此方法不起作用的示例吗?

编辑:此测试检查每个可能的字符以查看 indexOf 的行为是否符合预期。即对于每个可能的字符都相同。

for(int i=Character.MIN_VALUE;i<= Character.MAX_VALUE;i++) {
    String find = new String(new char[] {(char) i});
    String str = new String(new char[] {(char) (i+1), (char) i});
    String str1 = new String(new char[] {(char) (i+1)});

    int test1 = str.indexOf(find);
    if (test1 != 1)
        throw new AssertionError("test1 failed i="+i);

    int test2 = str1.indexOf(find);
    if (test2 != -1)
        throw new AssertionError("test2 failed i="+i);
}

没有发现任何差异。

indexOf and contains don't use character encoding of any sort and you can use characters which are not used in your character encoding for example. i.e. it is ignored for these functions.

All String.indexOf() and contains do is compare character for character. I am not sure what behaviour you are expecting for 100% Arabic support. Here is a simplified version what the indexOf()/contains() does

public static int indexOf(String string, char[] chars) {
    LOOP:
    for (int i = 0; i < string.length() - chars.length; i++) {
        for (int j = 0; j < chars.length; j++)
            if (string.charAt(i + j) != chars[j])
                continue LOOP;
        return i;
    }
    return -1;
}

public static void main(String args[]) {
    char[] chars = "test".toCharArray();
    String one = "qqwtestq";
    String two = "qwqtqwe";
    String str = new String(chars);
    System.out.println("indexOf(" + one+", " + Arrays.toString(chars) + ") = " + indexOf(one, chars));
    System.out.println(one + ".indexOf(" + str + ") = " + one.indexOf(str));
    System.out.println("indexOf(" + two+", " + Arrays.toString(chars) + ") = " + indexOf(two, chars));
    System.out.println(two + ".indexOf(" + str + ") = " + two.indexOf(str));

    char[] chars2 = { '\uffff', '\uFeFF' };
    String test = "qqw\uffff\uFeFFq";
    String str2 = new String(chars2);
    System.out.println("indexOf(" + test+", " + Arrays.toString(chars2) + ") = " + indexOf(test, chars2));
    System.out.println(test + ".indexOf(" + str2 + ") = " + test.indexOf(str2));
}

Prints

indexOf(qqwtestq, [t, e, s, t]) = 3
qqwtestq.indexOf(test) = 3
indexOf(qwqtqwe, [t, e, s, t]) = -1
qwqtqwe.indexOf(test) = -1
indexOf(qqw??q, [?, ?]) = 3
qqw??q.indexOf(??) = 3

Can you provide an example where this method doesn't work?

EDIT: This test checks every possible character to see if indexOf behaves as expected. i.e. the same for every possible character.

for(int i=Character.MIN_VALUE;i<= Character.MAX_VALUE;i++) {
    String find = new String(new char[] {(char) i});
    String str = new String(new char[] {(char) (i+1), (char) i});
    String str1 = new String(new char[] {(char) (i+1)});

    int test1 = str.indexOf(find);
    if (test1 != 1)
        throw new AssertionError("test1 failed i="+i);

    int test2 = str1.indexOf(find);
    if (test2 != -1)
        throw new AssertionError("test2 failed i="+i);
}

Finds no discrepancies.

冰之心2024-11-03 04:13:58

实施 KMP!
http://en.m.wikipedia.org /wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

编辑
抱歉,我不知道 Android 上的阿拉伯语。一些建议指向Cyanogen,并且只有 Android 3.0 支持阿拉伯语。

Implement KMP!
http://en.m.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

EDIT
Sorry, I did not know about Arabic on Android. Some suggestions point to Cyanogen, and that only Android 3.0 supports Arabic.

零度℉2024-11-03 04:13:58

尝试 StringUtils contains 方法。

Try StringUtils contains method.

鸠书2024-11-03 04:13:58

这个怎么样?

    char[] ch = { 't', 'e', 's', 't' };

    String string1 = "qqwtestq";
    if (string1.contains((new StringBuffer()).append(ch)))
        System.out.println("true");
    else
        System.out.println("false");

How about this?

    char[] ch = { 't', 'e', 's', 't' };

    String string1 = "qqwtestq";
    if (string1.contains((new StringBuffer()).append(ch)))
        System.out.println("true");
    else
        System.out.println("false");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文