C+ 的 Java 等效项 +'s “std::string::find_first_not_of”

标签 java string

Java 中是否有与 C++ 的“std::string::find_first_not of”等效的东西?

我有:

std::string path = "some path";
std::string eol = "some text";

size_t nextLinePos = path.find_first_not_of("\r\n", eol);

如何在 Java 上执行此操作?

Ps:对于 std::find_first_of 我使用这个函数:

    public static int findFirstOf(String findIn, String letters, int position)
    {
         Pattern pattern = Pattern.compile("[" + letters + "]");
         Matcher matcher = pattern.matcher(findIn);
         if (matcher.find()) 
         {
             position = matcher.start();
         }

         return position;
    }

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

最佳答案

您可以用非常简单的方式编写:

static int findFirstNotOf(String in, String notOf, int from) {
    for (int i = from; i < in.length(); ++i) {
        if (notOf.indexOf(in.charAt(i)) == -1) {
            return i;
        }
    }
    return -1;
}

我指的是描述的函数 here .

关于C+ 的 Java 等效项 +'s “std::string::find_first_not_of”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60710385/

相关文章:

html - 如何允许在传递给 HTML 值属性的字符串中使用双引号和单引号?

java - GlassFish 和应用程序客户端 Web 启动 : invalid Http response

java - 在 Java 中,Log 和 Log10 之间有性能差异吗?

java - 从 SQLite 数据库中检索数据到 EditText

java - OSGi bundle - 获取 DataSourcePool 以在 Adob​​e CQ5 中的 servlet 中使用

python - 如何在数据框中查找其间带有下划线文本的字符串

有偏差的 Java 递归三角形

java - 将字符串替换为我想要的内容时遇到问题

java - 将 Double 转换为特定形式

java - 从另一个字符串数组中过滤字符串数组