简体   繁体   English

如何从 Java 字符串的末尾和开头删除特殊字符

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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