在字符串中搜索并获取相等的字符

我需要在另一个逐个字符的字符串中搜索某个字符串,如果字符相同,则获取这样的字符;


我正在这样做


public String searchForSignature(String texto2) throws NoSuchAlgorithmException {

        String myString = "", foundString = "";

        myString = "aeiousrtmvb257";

        for (int i = 0; i < texto2.length() || i <= 1000; i++) {

            char c = texto2.charAt(i);


            for (int j = 0; j < myString.length(); j++) {

                if (c == myString.charAt(j)) {

                    foundString = foundString + c;

                }

            }

        }

        return foundString;

}

我想提高性能,看到有表格和使用正则表达式,因为我还是有点外行,我无法以我的方式成功。


 public String searchForSignature2(String texto2) {

        Pattern pattern = Pattern.compile("aeiousrtmvb257");

        Matcher matcher = pattern.matcher(texto2);

        while (matcher.find()) {

            System.out.println(matcher.group(1));

        }

        return matcher.group(1).toString();

    }

不返回任何东西


//编辑


真的,我想我对这个问题不是很清楚。实际上我需要让字符串中的所有字符等于“aeiousrtmvb257”


我就是这么弄的,现在好像还可以,就是不知道性能好不好。


 public String searchForSignature2(String texto2) {

        String foundString = "";

        Pattern pattern = Pattern.compile("[aeiousrtmvb257]");

        Matcher matcher = pattern.matcher(texto2);

        while (matcher.find()) {

            System.out.println(matcher.group());

            foundString+=matcher.group();

        }

        return foundString;

    }

}


一只名叫tom的猫
浏览 105回答 3
3回答

当年话下

据我了解您的问题,通过使用Pattern,Matcher这应该可以解决问题:代码private static final String PATTERN_TO_FIND = "[aeiousrtmvb257]";public static void main(String[] args) {&nbsp; &nbsp; System.out.println(searchForSignature2("111aeiousrtmvb257111"));}public static String searchForSignature2(String texto2) {&nbsp; &nbsp; Pattern pattern = Pattern.compile(PATTERN_TO_FIND);&nbsp; &nbsp; Matcher matcher = pattern.matcher(texto2);&nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; result.append(matcher.group());&nbsp; &nbsp; }&nbsp; &nbsp; return result.toString();}输出aeiousrtmvb257

呼如林

我不明白,你为什么要打印你找到的字符串public static String searchForSignature2(String texto2) {&nbsp; &nbsp; String maaString = "aeiousrtmvb257";&nbsp; &nbsp; String toSearch = ".*" + maaString +".*";&nbsp; &nbsp; boolean b = Pattern.matches(toSearch, texto2);&nbsp; &nbsp; return b ? maaString : "";&nbsp; }public static void main(String[] args){&nbsp; &nbsp; String input = "4erdhrAW BLBAJJINJOI WETSEKMsef saemfosnens3bntu is5o3n029j29i30kwq23eki4"+&nbsp; &nbsp; "maoifmakakmkakmsmfajiwfuanyi&nbsp; gaeniygaenigaenigeanige anigeanjeagjnageunega"+&nbsp; &nbsp; "movmmklmklzvxmkxzcvmoifsadoi asfugufngs"+&nbsp; &nbsp; "wpawfmaopfwamopfwampfwampofwampfawmfwamokfesomk"+&nbsp; &nbsp; "3rwq3rqrq3rqetgwtgwaeiousrtmvb2576266wdgdgdgdgd";&nbsp; &nbsp; String myString = searchForSignature2(input);&nbsp; &nbsp; System.out.println(myString);}你需要添加 .* 来告诉你的字符串被任何字符包围

慕桂英546537

我不知道背后的原因是什么texto2.length() || i <= 1000,但是根据您方法中的逻辑,我可以建议以下解决方案:public static void main(String... args) throws IOException {&nbsp; &nbsp; System.out.println(searchForSignature("hello"));}public static String searchForSignature(String texto2) {&nbsp; &nbsp; String myString = "aeiousrtmvb257";&nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; for (char s : texto2.toCharArray()) {&nbsp; &nbsp; &nbsp; &nbsp; if (myString.indexOf(s) != -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return builder.toString();}输出:eo
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java