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++;
}
# 1 楼答案
阅读整行内容并将其存储在
String line
中。然后,使用:# 2 楼答案
这里有两个问题,所以我只回答最后一个;当您拥有所需的
<text>
时,按如下方式操作:这将去掉分隔符
# 3 楼答案
您可以使用
Pattern
和Matcher
类<.*?>
李>