假设我有 java.util.Set<String>
的 "200Y2Z", "20012Y", "200829", "200T2K"
遵循相同的模式 "200$2$"
, 其中"$"
是占位符。现在,从 Java 中的此类字符串中获取一组唯一代码的最有效方法是什么?
输入:java.util.Set<String>
的 "200Y2Z", "20012Y", "200829", "200T2K"
预期输出:java.util.Set<String>
的 "YZ", "1Y", "89", "TK"
我的尝试::
public static void getOutPut()
{
Set<String> input = new HashSet<String>();
Set<String> output = new HashSet<String>();
StringBuffer out = null;
for(String in : input)
{
out = new StringBuffer();
StringCharacterIterator sci = new StringCharacterIterator(in);
while (sci.current( ) != StringCharacterIterator.DONE){
if (sci.current( ) == '$')
{
out.append(in.charAt(sci.getIndex()));
}
sci.next( );
}
output.add(out.toString());
}
System.out.println(output);
}
它工作正常,但还有比这更有效的方法吗?我需要为超过 1000K 的代码执行此操作。
最佳答案
获取模式中占位符的索引:
int i = pattern.getIndexOf('$');
您必须迭代才能获得所有索引:
pattern.getIndexOf('$', lastIndex+1);
循环和检查由您决定。
然后使用 charAt
和集合中每个元素的索引。
关于java - 如何从java中生成的模式中获取原始代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438667/