将C++ std::string的find_*_of()方法转换为Java方法。

问题描述 投票:1回答:3

当把代码从C++转换到Java时,有什么简单的方法可以把std::string方法转换为类似于 find_last_of(), find_last_not_of,等等?

这些C++方法可以找到一组字符中任何一个字符的索引。

Java的String类提供了 indexOf()lastIndexOf()但这些查找的是一个字符或一个字符串的索引,而不是一组字符的任何一个。

例如,下面的代码查找的是最后一个不是ASCII空格的字符。

size_t pos = myString.find_last_not_of( " \t\n\r" );
java
3个回答
5
投票

一个选择是使用Guava的 CharMatcher 类的转换。

下面是经过测试的单参数find_*_of()方法的转换。

public int findFirstOf( String sequence, String str ) {
    return CharMatcher.anyOf( str ).indexIn( sequence );
}

public int findFirstNotOf( String sequence, String str ) {
    return CharMatcher.anyOf( str ).negate().indexIn( sequence );
}

public int findLastOf( String sequence, String str ) {
    return CharMatcher.anyOf( str ).lastIndexIn( sequence );
}

public int findLastNotOf( String sequence, String str ) {
    return CharMatcher.anyOf( str ).negate().lastIndexIn( sequence );
}

欢迎提供其他答案。 [我找不到任何关于 find_last_not_of() 在stackoverflow和其他地方搜索时,发现Java中的CharMatcher。而我第一次在Guava中搜索相应的功能时,错过了CharMatcher。我想把这个简单的转换记录下来,以备将来使用]。


1
投票

如果你喜欢regex,你可以试试下面的等价物。这可能不是最有效的方法,但肯定值得考虑,如果你不想使用任何第三方库(鉴于,在Java的String类中没有等价的方法)。

P.S:- 如果你对第三方库很满意,那么我不建议使用regex来完成这个任务,因为这可能很快就会变得难以按要求扩展。

所以,这只是另一种选择。

public int findFirstOf( String sequence, String str ) {
    String regex = "^[^" + Pattern.quote(str) + "]*";
    int index = sequence.length() - sequence.replaceAll(regex, "").length();
    return index == sequence.length() ? -1 : index;
}

public int findFirstNotOf( String sequence, String str ) {
    String regex = "^[" + Pattern.quote(str) + "]*";
    int index = sequence.length() - sequence.replaceAll(regex, "").length();
    return index == sequence.length() ? -1 : index;
}

public int findLastOf( String sequence, String str ) {
    String regex = "[^" + Pattern.quote(str) + "]*$";
    return sequence.replaceAll(regex, "").length() - 1;
}

public int findLastNotOf( String sequence, String str ) {
    String regex = "[" + Pattern.quote(str) + "]*$";
    return sequence.replaceAll(regex, "").length() - 1;
}

我还没有测试上述方法。你可以做一下测试,然后把结果和你得到的相应方法进行比较,看看是否可行。如果不行,请回复。

至于第三方库,你也有Apache Commons。StringUtils 类,有以下方法。


-2
投票
static int findFirstNotOf(String searchIn, String searchFor, int searchFrom) {
    boolean found;
    char c;
    int i;
    for (i = searchFrom; i < searchIn.length(); i++) {
        found = true;
        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 i;
}

static int findLastNotOf(String searchIn, String searchFor, int searchFrom) {
    boolean found;
    char c;
    int i;
    for ( i = searchFrom; i>=0; i--) {
        found = true;
        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 i;
}

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');
      }

      found = findLastNotOf(str,"abcdefghijklmnopqrstuvwxyz ",str.length()-1);
      if (found>=0) {
            System.out.print("The last non-alphabetic character is " + str.charAt(found));
            System.out.print(" at position " + found + '\n');
          }
      str = "Please, erase trailing white-spaces   \n";
      String whitespaces =  " \t\f\n\r";

      found = findLastNotOf(str,whitespaces,str.length()-1);
      if (found!=str.length()-1)
        str = str.substring(0,found+1);
      else
        str = "";            // str is all whitespace

     System.out.printf('['+ str +"]\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.