我想解析一个字符串,但要能够比较,例如
- 在位置x处的1个字符
- 在位置x处的2个字符
- ...
- 在位置x处的n个字符
如果失败了
- 在位置x+1处的1个字符
- 在位置x+1处的2个字符
- ...
- 在位置x+1处的n个字符
以此类推,直到找到匹配项或到达EOS。
使用数组很容易做到这一点,但是我想在一个方法中完成这一点,并返回一个缓冲区,该缓冲区具有指向下一个开始处理的位置的索引。我被告知CharBuffer是一个很好的解决方案,但我不确定。
编辑举例说明-不是可以编译的代码!
Main
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.
}