Trimming multiple characters in a string

My program reads some lines from a file that need to be processed after processing. The original text in the file looks like

A1DY^
BLKSS^
"GH67^"^

      

Where ^

is the space character I used for the demonstration. As you can see all the words at the end of the file with a space. Some words in a double quote. I want to keep these lines in my program

A1DY
BLKSS
GH67

      

In other words, I want to trim all spaces and double quotes. If I use str.trim();

it will remove the last space. So the third line will be "GH67^"

. I also used str.replaceAll("^\"|\"$", "");

double quotes for trimming. Result GH67^

. This means I need to trim it again.

Is there a better way to remove all spaces and double quotes at once? Please note, I don't want to extract alphanumeric characters. I want to trim special characters.

+3


source to share


2 answers


This will trim any number of quotes or spaces from the beginning or end of the string:



str = str.replaceAll("^[ \"]+|[ \"]+$", "");

      

+3


source


In a strict interpretation of your question description, you only want to remove deleted spaces, not spaces, and not other whitespace characters like tabs ( \t

).

Also, the strict trim function will only remove double quotes if both a leading and a trailing pair are found, and only one such set.

If double quotes are present, you must also remove the trailing spaces within the double quotes.

To accomplish all of this, strictly , in a single regex operation, do the following:

str = str.replaceFirst("^(\"?)(.*?) *\\1 *$", "$2");

      



This regex uses ^

and bindings $

to ensure that it only matches the entire string.

Master "

is optional and maps to Capture Group 1 if present. The trailing one is "

matched only when the leading is matched "

, and the leading is "

only "

matching if the trailing one is matched. This is done by \1

linking back to the optional presenter "

. If they match, they will be removed from the result.

No leading spaces are removed, but any trailing spaces before and / or after the optional trailing "

are removed.

Anything not removed is captured in group 2 and stored in the replacement string.

0


source







All Articles