【发布时间】:2011-11-23 10:44:44
【问题描述】:
对我正在尝试做的事情进行一些补充。
我想解析一个String,但要能够比较,例如
- x 位置 1 个字符
- x 位置有 2 个字符
- ...
- 位置 x 的 n 个字符
失败了
- x+1 位置 1 个字符
- x+1 位置的 2 个字符
- ...
- 位置 x+1 的 n 个字符
等
继续直到我得到一个匹配,或 EOS。
使用数组很容易做到这一点,但我想在一个方法中做到这一点,并返回一个缓冲区,其索引指向下一个开始处理的位置。我被引导相信 CharBuffer 是一个很好的解决方案,但我不确定。
EDIT 举个例子 - 不是完全可编译的代码!
主要
List template1 = new List();
List template2 = new List();
String exampleInput = "oneone two";
template1.add ( "one" );
template1.add ( "two" );
template1.add ( "three" );
template2.add ( "one" );
template2.add ( "one" );
template2.add ( "two" );
Set templates = new Set();
templates.add ( template1 );
templates.add ( template2 );
NoisyParser np = new NoisyParser();
np.parse( templates, exampleInput );
NoisyParser
void parse( Set templates, Sting inp ){
iterate over each template that matches inp{
find_match( template, inp );
}
}
boolean find_match( template, inp ) {
This is where I need the magic to work out if the current element
in template matches the current position of inp, or inp+1, give or take.
}
【问题讨论】:
-
您的具体要求是什么?比较两个字符串并找到它们不匹配的第一个位置?还是比较 n 个字符串?
-
这有点复杂,我有许多字符串列表,我想将它们与嘈杂的输入字符串进行匹配,看看哪个列表匹配,是任意的。
-
我认为最好提供一个小例子。比如说,一个包含两个字符串的列表,另一个包含三个字符串和输入字符串的列表,然后指定您期望的结果究竟是什么。从帖子中很难理解您的确切要求。
标签: java arrays string parsing