有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

charat如何在小于和大于之间查找文本,然后在Java中剥离<>?

我不知道怎么找到这些词。。例如,我有这个文本

The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> .

当我在google上搜索<>时,我不知道搜索原因是什么,它将被忽略。需要如何获取此字符串的帮助

所以我将得到<location><plural-noun><location><adjective><location>

我必须使用charAt()方法。我的尝试:

String string = this.fileName;
for(int i = 0; i < string.length(); i++)
                if((string.charAt(i) == '<') && (string.charAt(i) == '>'))
                    System.println(""); //<-------- IM STUCK HERE

我不知道。。。几乎两天没有睡觉

我现在还有最后一个问题。。。如何删除显示的每个单词上的<>

String string = this.template;
        Pattern pattern = Pattern.compile("<.*?>");
        Matcher matcher = pattern.matcher(string);

        List<String> listMatches = new ArrayList<String>();

        while(matcher.find()) {
            listMatches.add(matcher.group());
        }
        // System.out.println(listMatches.size());
        int indexNumber = 1;
         for(String s : listMatches) {
             System.out.println(Integer.toString(indexNumber) + ". " + s);
             indexNumber++;
         }

共 (3) 个答案

  1. # 1 楼答案

    阅读整行内容并将其存储在String line中。然后,使用:

    String line = "The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> ."; 
    
    boolean found = false;
    String data[] = new String[20];
    int counter = 0;
    
    Arrays.fill(data, "");
    
    for(int i = 0; i < line.length() && counter < 20; i++) {
        if(line.charAt(i) == '<')
            found = true;
        else if(line.charAt(i) == '>' && found) {
            found = false;
            counter++;
        }
        else if(found) {
            data[counter] += line.charAt(i);
        }
    }
    
    for(int i = 0; i < counter; i++)
        System.out.println("Scanned data #" + (i + 1) + " = " + data[i]);
    
  2. # 2 楼答案

    这里有两个问题,所以我只回答最后一个;当您拥有所需的<text>时,按如下方式操作:

    String text = "<the_text_you_want>";
    
    text.replace("<","").replace(">","").replace("-"," ");
    

    这将去掉分隔符

  3. # 3 楼答案

    您可以使用PatternMatcher

    1. 搜索正则表达式模式<.*?>
    2. 使用Matcher查找模式