Direkz
Direkz

Reputation: 85

how to remove dash(-) in string using c#?

   Regex rgx2 = new Regex("[^[0-9] . \n\r\t]");

   string  dash = Regex.Replace(Des_AccNo.ToString(), @" ^-");

Upvotes: 0

Views: 34989

Answers (5)

Riyaz Hameed
Riyaz Hameed

Reputation: 1103

I would basically create a static class that automatically pops up against any string. If the same is GUID, you can simply do

Guid.NewGuid().ToString("N") returns only characters 

Input: 12345678-1234-1234-1234-123456789abc Output: 12345678123412341234123456789abc

    public static string ToNonDashed(this string input)
    {
        return input?.Replace("-", string.Empty);
    }

Upvotes: 4

Jim Mischel
Jim Mischel

Reputation: 134055

If you want to remove all non-numeric characters:

string result = Regex.Replace(inputString, @"[^0-9]", "");

Basically what that says is "if the character isn't a digit, then replace it with the empty string." The ^ as the first character in the character group negates it. That is, [0-9] matches any digit. [^0-9] matches everything except a digit. See Character Classes in the MSDN documentation.

The expression @"[^\d]" also would work

Upvotes: 13

Lokesh
Lokesh

Reputation: 11

string dash = Des_AccNo.ToString().Replace("-", string.Empty);

Upvotes: 1

Saket Kumar
Saket Kumar

Reputation: 4845

You can try this:

 Des_AccNo = Des_AccNo.Replace("-", string.Empty);

Upvotes: 1

Ali Sepehri.Kh
Ali Sepehri.Kh

Reputation: 2508

I don't know what is your code, but you can do that by:

val = val.Replace("-", string.Empty)

Upvotes: 24

Related Questions