Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to place - in a string

Tags:

string

c#

I have a string "8329874566". I want to place - in the string like this "832-98-4566"

Which string function can I use?

like image 671
Novice Developer Avatar asked Nov 26 '22 21:11

Novice Developer


2 Answers

I would have done something like this..

  string value = "8329874566";
  value = value.Insert(6, "-").Insert(3, "-");
like image 98
John Kraft Avatar answered Nov 29 '22 11:11

John Kraft


You convert it to a number and then format the string. What I like most about this is it's easier to read/understand what's going on then using a few substring methods.

string str = "832984566";
string val = long.Parse(str).ToString("###-##-####");
like image 36
Crispy Avatar answered Nov 29 '22 13:11

Crispy