I have a string like this –
"fruit=apple man=human abc=123"
I want values to be printed like –
fruit=
apple
man=
human
abc=
123
i.e. I also want to see the delimeter values. Currently I’m trying-
String status2="fruit=apple man=human abc=123";
Scanner scn = new Scanner(status2).useDelimiter("[a-z]*=+");
while(scn.hasNext())
{
System.out.println(scn.next());
System.out.println(scn.delimiter());
}
But I cannot see the delimeter values
apple
[a-z]*=+
human
[a-z]*=+
123
[a-z]*=+
Updated String –
"cobdate=01/28/2013 fundsnotextracted= elapsedtime=00:06:02 user=dataprod starttime=Wed, 30 Jan 2013 11:50:30 periods=DAILY, MTD, YTD knowledgedate=01/30/2013:11:50:10 progress=67 statusstep=Generating Reports ....."
Expected output –
cobdate=01/28/2013
fundsnotextracted=
elapsedtime=00:06:02
user=dataprod
starttime=Wed, 30 Jan 2013 11:50:30
periods=DAILY, MTD, YTD
knowledgedate=01/30/2013:11:50:10
progress=67
statusstep=Generating Reports .....
Your delimiter is not correct. You should set your delimiter after each
=sign, and also at everywhitespace. You can use this insead: –Here the delimiter is: – empty string following every
=denoted by(?<==)or a whitespace denoted by[ ].However, given your input string, and the required output, I would rather
splitthe string, with the samepatternas I used in thedelimiter, which will give you an array, that you can also use somewhere else later on: –Update: –
For your updated input, you would have to do some more work. First of all, you strictly need a
splithere. Plus, you would have to do the split twice – once on a whitespace, and another on a=.Now, your whitespace must be followed by a sequence of alphabets ending with
=, so that you don’t mistakenly split on the whitespace contained in the values. So, your code should look like this: –Output: –