How to put - in a string

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

What lowercase function can I use?

+4
source share
12 answers

There may be a complex, almost unreadable solution to regular expressions, but it is fairly readable and easy.

The first parameter of the .Substring () method is where you start receiving the characters, and the second is the number of characters you want to receive, and not giving it the default job as value.length -1 (get chars to the end of the line):

String value = "8329874566"; String Result = value.Substring(0,3) + "-" + value.Substring(3,2) + "-" + value.Substring(6); 

- [edit] -

Just noticed that you did not use one of the AT ALL numbers (the number "7") in the expected example of the result you gave, but if you want, just change the last substring as "5", and if you want "7", but not want 5 numbers in the last set, let it look like "5.4".

+8
source

I would do something like this.

  string value = "8329874566"; value = value.Insert(6, "-").Insert(3, "-"); 
+15
source

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

 string str = "832984566"; string val = long.Parse(str).ToString("###-##-####"); 
+13
source

Are you trying to do it like US social security numbers? Ie, with a hyphen after the third and fifth digits? If yes:

 string s = "8329874566"; string t = String.Format("{0}-{1}-{2}", s.Substring(0, 3), s.Substring(3, 2), s.Substring(5)); 
+5
source

Just from completeness, a variant of the regular expression:

 Regex.Replace(s, @"(\d{3})(\d{2})(\d{4})", "$1-$2-$3"); 

I believe the Insert option is the cleanest.

+5
source

This works great, and I think it is more clear:

  String value = "8329874566"; value = value.Insert(3, "-").Insert(6, "-"); 

The console outputs show this: 832-98-74566

+2
source
 var result = string.Concat(value.Substring(0,3), "-", value.Substring(3,2), "-", value.Substring(5,4)); 

or

 var value = "8329874566".Insert(3, "-").Insert(6, "-"); 
0
source

If the hyphen should go in the same place every time, then you can simply combine parts of the original string as follows:

 // 0123456789 <- index string number = "8329874566"; string new = number.Substring(0, 3) + "-" + number.Substring(3, 2) + "-" + number.Substring(5); 

For a general way to create mutable strings, use the StringBuilder class. This allows deletion and insertion before calling ToString to create the final string.

0
source

You can try the following:

 string strNumber = "8329874566" string strNewNumber = strNumber.Substring(0,3) + "-" + strNumber.Substring(4,2) + "-" strNumber.Substring(6) 

or something like that

0
source
 string val = "832984566"; string result = String.Format("{0}-{1}-{2}", val.Substring(0,3), val.Substring(3,2), val.Substring(5,4)); 
0
source

A simple (but not flexible) approach will loop over the characters of the string, while maintaining a counter. Then you can create a new string character by character. You can add a “-” after the third and fifth characters.

A better approach would be to use a function to insert a single character in the middle of a line with a specific index. String.Insert () will be fine. The only thing to pay attention to here is that the row indices converge with each insert.

CHANGE more language matching comments

0
source

Now how about this for a general solution?

 // uglified code to fit within horizontal limits public static string InsertAtIndices (this string original, string insertion, params int[] insertionPoints) { var mutable = new StringBuilder(original); var validInsertionPoints = insertionPoints .Distinct() .Where(i => i >= 0 && i < original.Length) .OrderByDescending(i => i); foreach (int insertionPoint in validInsertionPoints) mutable.Insert(insertionPoint, insertion); return mutable.ToString(); } 

Using:

 string ssn = "832984566".InsertAtIndices("-", 3, 5); string crazy = "42387542342309856340924803" .InsertAtIndices(":", 1, 2, 3, 4, 5, 6, 17, 200, -1, -1, 2, 3, 3, 4); Console.WriteLine(ssn); Console.WriteLine(crazy); 

Conclusion:

832-98-4566
4: 2: 3: 8: 7: 5: 42342309856: 340924803

Overkill? Yes, may be...

<sub> PS Yes, I am a regular hero illiterate - something I hope to fix someday.

0
source

Source: https://habr.com/ru/post/1303971/


All Articles