我的搜索方法有问题。
使用这种方法,我可以在文本字段中输入一个单词,然后在文本区域中显示该单词。但是,如果我让它运行,只会发生一次。我需要对其进行扩展,以便每次单击“输入”时,程序都应继续在文本区域中进行搜索。我怎样才能做到这一点? 并且请给我代码示例。我只剩下2天的演讲时间。
非常感谢您的帮助
unsigned hextodigit(int hex)
{
unsigned result = 0;
switch(toupper(hex))
{
case '0':
return 0;
break;
case '1':
return 1;
break;
case '2':
return 2;
break;
case '3':
return 3;
break;
case '4':
return 4;
break;
case '5':
return 5;
break;
case '6':
return 6;
break;
case '7':
return 7;
break;
case '8':
return 8;
break;
case '9':
return 9;
break;
case 'A':
return 10;
break;
case 'B':
return 11;
break;
case 'C':
return 12;
break;
case 'D':
return 13;
break;
case 'E':
return 14;
break;
case 'F':
return 15;
break;
default:
break;
}
}
unsigned fromhex4chars(const char *str)
{
return hextodigit(*(str + 3)) + (hextodigit(*(str + 2)) << 4) + (hextodigit(*(str + 1)) << 8) + (hextodigit(*str) << 12);
}
void convert(const char *str, unsigned *buff)
{
size_t len = strlen(str) / 4;
while(len--)
{
*buff++ = fromhex4chars(str);
str += 4;
}
}
int main(int argc, char **argv)
{
unsigned x[100];
convert(argv[1], x);
return 0;
}
答案 0 :(得分:0)
有一个overloaded version of the indexOf
method,可让您从特定的索引开始搜索。跟踪最后找到的索引,然后从该位置开始搜索:
@Override
public void start(Stage primaryStage) throws Exception {
TextField textField = new TextField("foo");
TextArea textarea = new TextArea();
for (int i = 0; i < 10; i++) {
textarea.appendText("foo\nbarfoobarfoofoo\n");
}
textField.setOnAction(evt -> {
String searchText = textField.getText();
if (searchText.isEmpty()) {
return; // searching for empty text doesn't make sense
}
int index = textarea.getSelection().getEnd();
// in case of the first search, start at the beginning
// TODO: adjust condition/starting index according to needs
if (textarea.getSelection().getLength() == 0) {
index = 0;
}
// find next occurrence
int newStartIndex = textarea.getText().indexOf(searchText, index);
// mark occurrence
if (newStartIndex >= 0) {
textarea.selectRange(newStartIndex, newStartIndex + searchText.length());
}
});
Scene scene = new Scene(new VBox(textField, textarea));
primaryStage.setScene(scene);
primaryStage.show();
}
如果您对选择后的元素不满意(或者在光标之后,如果未选择范围),则可以保存最后匹配末尾的数据:
@Override
public void start(Stage primaryStage) throws Exception {
TextField textField = new TextField("foo");
TextArea textarea = new TextArea();
for (int i = 0; i < 10; i++) {
textarea.appendText("foo\nbarfoobarfoofoo\n");
}
class SearchHandler implements EventHandler<ActionEvent> {
int index = 0;
@Override
public void handle(ActionEvent event) {
String searchText = textField.getText();
String fullText = textarea.getText();
if (index + searchText.length() > fullText.length()) {
// no more matches possible
// TODO: notify user
return;
}
// find next occurrence
int newStartIndex = textarea.getText().indexOf(searchText, index);
// mark occurrence
if (newStartIndex >= 0) {
index = newStartIndex + searchText.length();
textarea.selectRange(newStartIndex, index);
} else {
index = fullText.length();
// TODO: notify user
}
}
}
SearchHandler handler = new SearchHandler();
textField.setOnAction(handler);
// reset index to search from start when changing the text of the TextField
textField.textProperty().addListener((o, oldValue, newValue) -> handler.index = 0);
Scene scene = new Scene(new VBox(textField, textarea));
primaryStage.setScene(scene);
primaryStage.show();
}