Hi I am looking for a solution with checks out if the string ends with a special character, If yes then remove it. The new string after removing special character should also not contain special character.
The special characters should be in array [!@#$%&/{()}=?+] to check against in end of string.
How about using String.TrimEnd? This method is specifically for removing the characters in a given array from the string.
var newstr = s.TrimEnd("[!@#$%&/{()}=?+]".ToCharArray());
Also, the following requirement looks like you should be using String.Replace to replace any unwanted character in your string:
The new string after removing special character should also not contain special character
In that case you should do something like this:
string unwanted = "[!@#$%&/{()}=?+]";
for (int i = 0; i < unwanted.Length; i++)
s = s.Replace(unwanted.Substring(i, 1), "");
Which removes all occurrances of all unwanted characters from the entire string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With