简体   繁体   中英

How to remove only starting and ending special character from a string?

I want to remove only the starting special character ie " and ending special character ". The given input string is

String input= "\"This#string%contains^special*characters&.\"";

The expected output is

String output= "This#string%contains^special*characters&.";

Can any one help me how to do this using regex or any other way to remove only starting and ending special character only from the input string in java.

I would use a regex approach:

String input= "\"This#string%contains^special*characters&.\"";
String output = input.replaceAll("^[^A-Za-z0-9]|[^A-Za-z0-9]$", "");
System.out.println(output);

This prints:

This#string%contains^special*characters&.

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