How to break a string when a word was found
I have data in my String object which is below
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
Now when I read each line, if I find words (Inlist, NotEqual, Inlist) then it needs to break the line and require the next line.
Output would be:
Scanner
Resolved scan set
Area of intrest
Initial responder
So far, what I have tried is
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);
}
}
}
source to share
Use this line:
StringTokenizer str=new StringTokenizer(filterstringobj," ");
instead
StringTokenizer str=new StringTokenizer(filterstringobj,"");
EDIT
So check out the following demo code:
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());
}
}
source to share
Very flexible, just one line:
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"));
}
This will print:
Set of allowed scans
Interior area
Final initial responder
source to share
You don't need to use StringTokenizer. Plese see the old question to find out why?
Instead of a StringTokenizer, you can use a regular expression to match parts of a string you don't need and replace them with an empty string.
source to share
Three problems in the code:
StringTokenizer str=new StringTokenizer(filterstringobj,"");
which should be
StringTokenizer str=new StringTokenizer(filterstringobj," ");
Second:
if(Inlistremove.equalsIgnoreCase("InList") &&
Inlistremove.equalsIgnoreCase("NotEqual") &&
Inlistremove.equalsIgnoreCase("Equal")
)
which should be:
if(Inlistremove.equalsIgnoreCase("InList") ||
Inlistremove.equalsIgnoreCase("NotEqual") ||
Inlistremove.equalsIgnoreCase("Equal")
)
Third, how do you go to the next line of the string array? You have to modify the code slightly to include moving to another line for parsing.
For this, I suggest you create a function:
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;
}
}
}
}
and in the method main()
you do the following:
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]);
}
}
source to share