Sunday, 27 August 2017

java - How can I trim beginning and ending double quotes from a string?



I would like to trim a beginning and ending double quote (") from a string.
How can I achieve that in Java? Thanks!


Answer



You can use String#replaceAll() with a pattern of ^\"|\"$ for this.




E.g.



string = string.replaceAll("^\"|\"$", "");


To learn more about regular expressions, have al ook at http://regular-expression.info.



That said, this smells a bit like that you're trying to invent a CSV parser. If so, I'd suggest to look around for existing libraries, such as OpenCSV.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...