JohnDown
JohnDown

Reputation: 17

c# removing commas

How to remove unnecessary comma's in string?

e.g

textbox.text = "1,23,45,67";

What I want is, to remove second and third comma and next one if appears, e.g

textbox.text = "1,234567"; 

Or in other way, how to allowed to place only one comma?

Upvotes: 0

Views: 95

Answers (2)

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

This one is working like you expected (don't forget to add using System.Linq;):

string[] Splitted = textbox.Text.Split(',');
textbox.Text = string.Join(",", Splitted.Take(2)) + string.Join("", Splitted.Skip(2));

123 -> 123

123,123 -> 123,123

123,123,123 -> 123,123123

Upvotes: 1

Giorgi_Mdivani
Giorgi_Mdivani

Reputation: 383

here you can use string trim method, and you can see more detailed info over here: https://msdn.microsoft.com/en-us/library/d4tt83f9(v=vs.110).aspx

Upvotes: 0

Related Questions