简体   繁体   中英

how to remove special character from string?

my current scenario is , I have a string like "XYZName" but it should take only first 3 character and in those 3 character there shouldn't be any special character.

Example : "XYZName" result should be: XYZ

and another requremnt is Example : "X.YZName" or "XY-ZName" result should be: XYZ

first example is done but not able to implement second example. my code is

 comName = (comName .Replace(".", string.Empty).Length >= 3
                    ? comName .Replace(" ", string.Empty).Substring(0, 3)
                    : comName .Replace(" ", string.Empty)).ToUpper();

how to do this ?? Thanks in advance

Update after comment that digits are also allowed

comName = Regex.Replace(comName, @"[^a-zA-Z\d]", "").Substring(0, 3);

The regular expression uses a negated set, so any character other than az , AZ or a digit ( \\d ) will be removed.

After the characters are removed, Substring takes the first 3.


If you're using C#8+ you could replace the Substring with a Range:

comName = Regex.Replace(comName, @"[^a-zA-Z\d]", "")[..3];

You can make sure to first match 3 times a char AZ with optional chars other than AZ or a whitespace char in between:

^[A-Z][^\sA-Z]*[A-Z][^\sA-Z]*[A-Z]

See a regex demo for the matches.

Then from those matches, remove all chars other than AZ:

var regex = new Regex(@"^[A-Z][^\sA-Z]*[A-Z][^\sA-Z]*[A-Z]");
string[] strings = {"XYZName", "X.YZName", "XY-ZName"};

foreach (String s in strings)
{
    var m = regex.Match(s);
    if (m.Success) {
        Console.WriteLine(Regex.Replace(m.Value, @"[^A-Z]+", ""));
    }
}

Output

XYZ
XYZ
XYZ

See a C# demo .


Or you can use 3 capture groups and directly print the value of the groups.

^([A-Z])[^\sA-Z]*([A-Z])[^\sA-Z]*([A-Z])

C# demo | Regex demo

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