Java等效于C ++的“ std :: string :: find_first_not_of”

时间:2020-03-16 17:17:16

标签: java string

C ++的“ std :: string :: find_first_not of”是否有Java等效项?

我有:

0x55555555

我如何在Java上做到这一点?

Ps:对于std::string path = "some path"; std::string eol = "some text"; size_t nextLinePos = path.find_first_not_of("\r\n", eol); ,我使用此功能:

std::find_first_of

也许这里需要改变一些东西?

2 个答案:

答案 0 :(得分:2)

我认为您可以用一种非常简单的方式来写:

static int findFirstNotOf(String searchIn, String searchFor, int searchFrom) {
    char[] chars = searchFor.toCharArray();
    boolean found;
    for (int i = searchFrom; i < searchIn.length(); i++) {
        found = true;
        for (char c : chars) {
            if (searchIn.indexOf(c) == -1) {
                found = false;
            }
        }
        if (!found) {
            return i;
        }
    }
    return -1;
}

我指的是here中描述的功能。

答案 1 :(得分:-1)

static int findFirstNotOf(String searchIn, String searchFor, int searchFrom) {
   // char[] chars = searchFor.toCharArray();
    boolean found;
    char c;
    for (int i = searchFrom; i < searchIn.length(); i++) {
        found = true;
       // for (char c : chars) {
        c = searchIn.charAt(i);
            System.out.printf("s='%s', idx=%d\n",c,searchFor.indexOf(c));
            if (searchFor.indexOf(c) == -1) {
                found = false;
            }
      //  }
        if (!found) {
            return i;
        }
    }
    return -1;
}

public static void main(String[] args){
    String str =  "look for non-alphabetic characters...";

     int  found = findFirstNotOf(str,"abcdefghijklmnopqrstuvwxyz ",0);

      if (found!=str.length()) {
        System.out.print("The first non-alphabetic character is " + str.charAt(found));
        System.out.print(" at position " + found + '\n');
      }
}