我的String对象中有一个数据,如下面的
Scanner Inlist 1,2,3
Resolved scan set NotEqual to Non Scan Set
Area of intrest equal to Total Intrest
Initial responder Inlist enter values
现在当我读到每一行时,如果我找到了单词(Inlist,NotEqual,Inlist),那么它需要打破这一行并需要读取下一行。
Output would be:
Scanner
Resolved scan set
Area of intrest
Initial responder
直到现在我所尝试的是
String filterstringobj=promtchild.toString();
StringTokenizer str=new StringTokenizer(filterstringobj,"");
while(str.hasMoreTokens())
{
String Inlistremove=str.nextToken("InList");
if(Inlistremove.length()!=0)
{
System.out.println(Inlistremove);
if(Inlistremove.equalsIgnoreCase("InList") &&
Inlistremove.equalsIgnoreCase("NotEqual") &&
Inlistremove.equalsIgnoreCase("Equal")
)
{
System.out.println(Inlistremove);
}
}
}
答案 0 :(得分:3)
你的逻辑有一个很大的缺陷:
看着你if
我看到了
if(Inlistremove.equalsIgnoreCase("InList")&&Inlistremove.equalsIgnoreCase("NotEqual")&&...
Inlistremove
如何等同于"InList"
AND 同时等于"NotEqual"
?您在寻找 OR 吗?那将是||
答案 1 :(得分:1)
使用此行:
StringTokenizer str=new StringTokenizer(filterstringobj," ");
而不是
StringTokenizer str=new StringTokenizer(filterstringobj,"");
修改强>
好的,然后观看以下演示代码:
import java.util.StringTokenizer;
class WordsFromString
{
public static void main(String st[])
{
String data = "Scanner Inlist 1,2,3\n"+
"Resolved scan set NotEqual to Non Scan Set\n"+
"Area of intrest equal to Total Intrest\n"+
"Initial responder Inlist enter values";
StringTokenizer tokenizer = new StringTokenizer(data,"\n",true);
StringBuilder output = new StringBuilder();
while (tokenizer.hasMoreElements())
{
String sLine = tokenizer.nextToken();
StringTokenizer tokenizerWord = new StringTokenizer(sLine," ",true);
while (tokenizerWord.hasMoreElements())
{
String word = tokenizerWord.nextToken();
if ("Inlist".equals(word) || "NotEqual".equals(word) || "Inlist".equals(word) || "equal".equals(word))
{
break;
}
else
{
output.append(word);
}
}
}
System.out.println(output.toString());
}
}
答案 2 :(得分:1)
非常灵活,只有一行:
public static String parseLine(String line){
return line.replaceAll("(?i)(inlist|notequal|equal).*", "");
}
public static void main(String[] a){
System.out.println(parseLine("Resolved scan set NotEqual to Non Scan Set"));
System.out.println(parseLine("Area of intrest equal to Total Intrest"));
System.out.println(parseLine("Initial responder Inlist enter values"));
}
将打印:
已解决的扫描集
兴趣区
Total Intrest Initial Responder
答案 3 :(得分:0)
您不需要使用StringTokenizer。 请注意an old question知道原因吗?
您可以使用正则表达式来匹配不需要的字符串部分,而不是StringTokenizer,而是用空字符串替换它们。
答案 4 :(得分:0)
代码中有三个问题:
StringTokenizer str=new StringTokenizer(filterstringobj,"");
应该是
StringTokenizer str=new StringTokenizer(filterstringobj," ");
其次是:
if(Inlistremove.equalsIgnoreCase("InList") &&
Inlistremove.equalsIgnoreCase("NotEqual") &&
Inlistremove.equalsIgnoreCase("Equal")
)
应该是:
if(Inlistremove.equalsIgnoreCase("InList") ||
Inlistremove.equalsIgnoreCase("NotEqual") ||
Inlistremove.equalsIgnoreCase("Equal")
)
第三个是,你将如何移动到字符串数组的下一行?您必须稍微更改代码以包含移动到另一个要解析的字符串。
为此,我建议你创建一个函数:
public static void Parse(String s){
String filterstringobj=s;
StringTokenizer str=new StringTokenizer(filterstringobj," ");
while(str.hasMoreTokens())
{
String Inlistremove=str.nextToken("InList");
if(Inlistremove.length()!=0)
{
System.out.println(Inlistremove);
if(Inlistremove.equalsIgnoreCase("InList") ||
Inlistremove.equalsIgnoreCase("NotEqual") ||
Inlistremove.equalsIgnoreCase("Equal")
)
{
System.out.println(Inlistremove);
return;
}
}
}
}
并在main()
方法中执行此操作:
public static void main(String[] args)
{
String[] array = new String[3];
array[0] = "Resolved scan set NotEqual to Non Scan Set";
array[1] = "Area of intrest equal to Total Intrest";
array[2] = "Initial responder Inlist enter values";
for(int i = 0; i < 3; i++) {
Parse(array[i]);
}
}