Line / number separation every Nth character / number?

I need to split the number into even parts, for example:
32427237 should become 324 272 37
103092501 should become 103 092 501

I'm sure I could just for the following numbers, but I'm sure there is a more efficient way, since I do not want to skip characters in these numbers - the numbers themselves can be of any length, so if the number was 1234567890 I would like it to be divided into these parts 123 456 789 0

I saw examples in other languages, such as Python, etc., but I don’t understand them well enough to convert them to C # - looping, although the characters and then in the third get the previous one and then this index to get the string section can do the job, but I'm open to suggestions on how to do this better.

+43
string split c #
Nov 09 '10 at 11:58
source share
12 answers

If you need to do this in many places in your code, you can create a fantastic extension method:

static class StringExtensions { public static IEnumerable<String> SplitInParts(this String s, Int32 partLength) { if (s == null) throw new ArgumentNullException("s"); if (partLength <= 0) throw new ArgumentException("Part length has to be positive.", "partLength"); for (var i = 0; i < s.Length; i += partLength) yield return s.Substring(i, Math.Min(partLength, s.Length - i)); } } 

Then you can use it as follows:

 var parts = "32427237".SplitInParts(3); Console.WriteLine(String.Join(" ", parts)); 

Conclusion 324 272 37 optional.

+83
Nov 09 '10 at 12:11
source share

You can use a simple loop to insert spaces at every nth position:

 string input = "12345678"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.Length; i++) { if (i % 3 == 0) sb.Append(' '); sb.Append(input[i]); } string formatted = sb.ToString(); 
+7
Nov 09 '10 at 12:11
source share

LINQ Rules:

 var input = "1234567890"; var parts = 3; var output = input.ToCharArray() .BufferWithCount(parts) .Select(c => new String(c.ToArray())); 

UPDATED:

 string input = "1234567890"; double parts = 3; int k = 0; var output = input .ToLookup(c => Math.Floor(k++ / parts)) .Select(e => new String(e.ToArray())); 
+4
Nov 09 '10 at
source share

One very simple way to do this (not the most efficient, but then not an order of magnitude slower than the most effective).

  public static List<string> GetChunks(string value, int chunkSize) { List<string> triplets = new List<string>(); while (value.Length > chunkSize) { triplets.Add(value.Substring(0, chunkSize)); value = value.Substring(chunkSize); } if (value != "") triplets.Add(value); return triplets; } 

Here is an alternative

  public static List<string> GetChunkss(string value, int chunkSize) { List<string> triplets = new List<string>(); for(int i = 0; i < value.Length; i += chunkSize) if(i + chunkSize > value.Length) triplets.Add(value.Substring(i)); else triplets.Add(value.Substring(i, chunkSize)); return triplets; } 
+3
Nov 09 '10 at 12:05
source share

Cleavage Method:

 public static IEnumerable<string> SplitInGroups(this string original, int size) { var p = 0; var l = original.Length; while (l - p > size) { yield return original.Substring(p, size); p += size; } yield return original.Substring(p); } 

To join as a string, separated by spaces:

 var joined = String.Join(" ", myNumber.SplitInGroups(3).ToArray()); 

Edit: I like Martin Liversage's solution :)

Edit 2: Bug fixed.

Edit 3: Added code for joining a line.

+2
Nov 09 '10 at 12:12
source share

I would do something similar, although I'm sure there are other ways. It should be performed very well.

 public static string Format(string number, int batchSize, string separator) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= number.Length / batchSize; i++) { if (i > 0) sb.Append(separator); int currentIndex = i * batchSize; sb.Append(number.Substring(currentIndex, Math.Min(batchSize, number.Length - currentIndex))); } return sb.ToString(); } 
+2
Nov 09 '10 at 12:12
source share

This is already half a decade, but:

 int n = 3; string originalString = "32427237"; string splitString = string.Join(string.Empty,originalString.Select((x, i) => i > 0 && i % n == 0 ? string.Format(" {0}", x) : x.ToString())); 
+2
Jul 12 '16 at 5:01
source share

I like that it's cool, although not super-efficient:

 var n = 3; var split = "12345678900" .Select((c, i) => new { letter = c, group = i / n }) .GroupBy(l => l.group, l => l.letter) .Select(g => string.Join("", g)) .ToList(); 
+1
Nov 09 '10 at 12:15
source share

It may be off topic, because I don’t know why you want to format the numbers in this way, so please just ignore this message if it is not relevant ...

As shown, an integer in different cultures. You must do this locally in an independent way so that it is easier to localize your changes at a later stage.

int.ToString accepts various parameters that can be used for formatting for different cultures. The "N" parameter provides a standard format for culture-specific groupings.

x string formatting is a great resource.

0
Nov 09 '10 at 12:12
source share

Try the following:

 Regex.Split(num.toString(), "(?<=^(.{8})+)"); 
0
Dec 16 '15 at 0:47
source share

If you know that the entire length of the string is exactly divided by the size of the part, use:

 var whole = "32427237!"; var partSize = 3; var parts = Enumerable.Range(0, whole.Length / partSize) .Select(i => whole.Substring(i * partSize, partSize)); 

But if there is a possibility that the whole line may have a fractional fragment at the end, you need a little more refinement:

 var whole = "32427237"; var partSize = 3; var parts = Enumerable.Range(0, (whole.Length + partSize - 1) / partSize) .Select(i => whole.Substring(i * partSize, Math.Min(whole.Length - i * partSize, partSize))); 

In these examples, the parts will be IEnumerable, but you can add .ToArray () or .ToList () at the end if you need a string [] or List <string> value.

0
Jul 03 '16 at 16:44
source share

Good implementation using answers from other StackOverflow questions:

 "32427237" .AsChunks(3) .Select(vc => new String(vc)) .ToCsv(" "); // "324 272 37" "103092501" .AsChunks(3) .Select(vc => new String(vc)) .ToCsv(" "); // "103 092 501" 

AsChunks (): https://stackoverflow.com/a/2208778

ToCsv (): stack overflow

0
Aug 26 '17 at 2:28
source share



All Articles