Vijay Birari
Vijay Birari

Reputation: 31

Replacing a “ - ” with a single space? Replace method not working in c# library

i was working on a personal program, and in it, i get some strings which may have a space hyphen space. or something like this: " - " and what I have to do is replace this with a single space. Now, the problem is, when i try to use a replace method from the c# library it doesn't seem to do anything. This is what i tried:

string firsttext = firsttextbox.Text.ToLower();
string name = firsttext.Replace(" - ", " ");

But this fails to replace the string in firsttext's space hyphen space pattern with a single space. So when i try to use this text for example:

Leasing‐Other

it just returns this into string name:

Leasing‐Other

however it should actually be returning this:

LeasingOther

Upvotes: 1

Views: 905

Answers (1)

Pavel Pája Halbich
Pavel Pája Halbich

Reputation: 1583

You have spaces in your searching pattern. Use

string firsttext = firsttextbox.Text.ToLower();
string name = firsttext.Replace("-", " ");

That will work.

If your data are inconsistent, resolve all cases by replacing all variants.

string name = firsttext.Replace("-", " ").Replace(" -", " ").Replace("- ", " ").Replace(" - ", " ");

Upvotes: 1

Related Questions