3 回答
TA贡献1890条经验 获得超9个赞
据我了解您的问题,通过使用Pattern,Matcher这应该可以解决问题:
代码
private static final String PATTERN_TO_FIND = "[aeiousrtmvb257]";
public static void main(String[] args) {
System.out.println(searchForSignature2("111aeiousrtmvb257111"));
}
public static String searchForSignature2(String texto2) {
Pattern pattern = Pattern.compile(PATTERN_TO_FIND);
Matcher matcher = pattern.matcher(texto2);
StringBuilder result = new StringBuilder();
while (matcher.find()) {
result.append(matcher.group());
}
return result.toString();
}
输出
aeiousrtmvb257
TA贡献1798条经验 获得超3个赞
我不明白,你为什么要打印你找到的字符串
public static String searchForSignature2(String texto2) {
String maaString = "aeiousrtmvb257";
String toSearch = ".*" + maaString +".*";
boolean b = Pattern.matches(toSearch, texto2);
return b ? maaString : "";
}
public static void main(String[] args)
{
String input = "4erdhrAW BLBAJJINJOI WETSEKMsef saemfosnens3bntu is5o3n029j29i30kwq23eki4"+
"maoifmakakmkakmsmfajiwfuanyi gaeniygaenigaenigeanige anigeanjeagjnageunega"+
"movmmklmklzvxmkxzcvmoifsadoi asfugufngs"+
"wpawfmaopfwamopfwampfwampofwampfawmfwamokfesomk"+
"3rwq3rqrq3rqetgwtgwaeiousrtmvb2576266wdgdgdgdgd";
String myString = searchForSignature2(input);
System.out.println(myString);
}
你需要添加 .* 来告诉你的字符串被任何字符包围
TA贡献1848条经验 获得超10个赞
我不知道背后的原因是什么texto2.length() || i <= 1000,但是根据您方法中的逻辑,我可以建议以下解决方案:
public static void main(String... args) throws IOException {
System.out.println(searchForSignature("hello"));
}
public static String searchForSignature(String texto2) {
String myString = "aeiousrtmvb257";
StringBuilder builder = new StringBuilder();
for (char s : texto2.toCharArray()) {
if (myString.indexOf(s) != -1) {
builder.append(s);
}
}
return builder.toString();
}
输出:eo
添加回答
举报