Skip to content
Advertisement

Read string value including spaces : Java

I need to read space-separated values from a file that is separated by a colon.

My file has this data –

JavaScript

Current implementation: I am copying all the values after delimiter (colon in my case) to a new file and reading values from the new file accordingly.

While copying entries to new file spaces are being ignored. In the above file “Not approved yet” is saved as only “Not”. How can I get the complete line? Here is my code –

JavaScript

Advertisement

Answer

This \S+ in regex "\b(Name |DOB | Application Status )\s*:\s*(\S+)"; gets only non-white space charcters. So it terminates after seeing space after "Not" value. Inorder to get full value after ":" change the \S+ to .* which gets any character except newline. So the regex becomes like this "\b(Name |DOB | Application Status )\s*:\s*(.*)". It gets all space after the value so trim the value before using it. So your code becomes like this

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement