简体   繁体   中英

How to Remove special Characters from Java String at the end and at the beginning

lets say i have a string like

String examplestring = "%,.&/)"hello, mynameis, Fatih-Mehmet?And yours.insidedot.2874d3"( )§("

And i would want to extract the core part of

String goalstring = "hello, mynameis, Fatih-Mehmet?And yours.insidedot 2874d3"

So keeping all special characters within the core but removing all special characters from the end and from the beginning of the string, how could I achieve that?

Thanks in Advance

Assuming you want to extract a single double quoted term from inside the string, surrounded by other possible content at the start or end, we could phrase your requirement as:

String examplestring = "%,.&/)\"hello, mynameis, Fatih-Mehmet!And yours?insidedot.2874d3\"(.)§(";
examplestring = examplestring.replaceAll("^[^\"]*\"|\"[^\"]*$", "");
System.out.println(examplestring);

// hello, mynameis, Fatih-Mehmet!And yours?insidedot.2874d3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM