加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C的“std :: string :: find_first_of”等效于Java

发布时间:2020-12-16 06:49:39 所属栏目:百科 来源:网络整理
导读:C的“std :: string :: find_first_of”是否有任何 Java等价物? string string1( "This is a test string!"); int location = string1.find_first_of( "aeiou" ); //location is now "2" (the position of "i") 实现同样功能的最简单方法是什么? 编辑:建
C的“std :: string :: find_first_of”是否有任何 Java等价物?

string string1( "This is a test string!");
 int location = string1.find_first_of( "aeiou" );
 //location is now "2" (the position of "i")

实现同样功能的最简单方法是什么?

编辑:建议的解决方案也必须适用于Android.

解决方法

不使用外部库:

String string = "This is a test string!";
     String letters = "aeiou";
     Pattern pattern = Pattern.compile("[" + letters + "]");
     Matcher matcher = pattern.matcher(string);
     int position = -1;
     if (matcher.find()) {
         position = matcher.start();
     }
     System.out.println(position); // prints 2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读