0
interview.Job.Summary.ToArray().split()

interview.Job.Summary is a string and if becomes a char [] if I use the toArray method. But it seems like there is not such function as split. I want it to split so that each array contains certain number of characters (200 chars -> 50 50 50 50)

Please help!

EDIT:

enter image description here

I am using a library called PDFSharp. To display a string, we have to literally draw the string. So, it seems impossible to make this long one line into 4 separate lines without splitting the char array.

EDIT2:

enter image description here

CharithJ
  • 46,289
  • 20
  • 116
  • 131
Dukakus17
  • 1,221
  • 4
  • 15
  • 32
  • 1
    Possible duplicate of [Create batches in linq](https://stackoverflow.com/questions/13731796/create-batches-in-linq) – Kote Jul 15 '17 at 03:54
  • Can you share the sample value of `interview.Job.Summary` and how you want the output should look like? – Chetan Jul 15 '17 at 04:21
  • Please don't do screenshots of text. Just copy the text and format it as code. – Eric Lippert Jul 15 '17 at 13:09

3 Answers3

2

You can do this:

string text = "It was the best of times, it was the worst of times,";
int lengthOfEachPart = 10;

string[] parts =
    text
        .ToCharArray()
        .Select((x, n) => new { x, n })
        .GroupBy(y => y.n / lengthOfEachPart)
        .Select(y => new string(y.Select(z => z.x).ToArray()))
        .ToArray();

That gives you:

It was the 
 best of t 
imes, it w 
as the wor 
st of time 
s, 
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

You don´t have different arrays there, it is one array of your string characters. it is hard to understand what you are trying to achieve

EDIT:

Oh I think I understand.. I think you misunderstood, it is already in an array, it means it is already split. You just need to use the array to get each char from it like this:

  int[] arr = interview.Job.Summary.ToArray();

  foreach (int i in arr) 
  {
     firstArray = arr.Take(array.Length / 4).ToArray();
     secondArray = arr.Take(array.Length / 4).ToArray();
     thirdArray = arr.Take(array.Length / 4).ToArray();
     fourthArray = arr.Take(array.Length / 4).ToArray();

     string1 = Take(firstArray.Length).ToString();
     string2 = Take(secondArray.Length).ToString();
     string3 = Take(thirdArray.Length).ToString();
     string4 = Take(fourthArray.Length).ToString();
  }
Mikael Puusaari
  • 854
  • 1
  • 10
  • 14
  • It is already splitted, I added code to show how to get each char from the array and then do what you want with each char however you like – Mikael Puusaari Jul 15 '17 at 04:30
  • they are like you wanted them to be 50, 50, 50, now you just need to use the foreach to iterate through them and then do whatever you want with each one – Mikael Puusaari Jul 15 '17 at 04:32
  • Edited my code a bit, hope this is what you are looking for – Mikael Puusaari Jul 15 '17 at 04:44
  • There are two options for u, either use the arrays or strings as you please – Mikael Puusaari Jul 15 '17 at 04:57
  • Thanks for your help. But as you see in my 2nd edit, it gives me an error. It says i cannot change char[] to int[] on the first line. – Dukakus17 Jul 15 '17 at 05:05
  • of course you cant change it at once from one kind of array to another, you have to first change it to another type.. otherwise it is like trying to change a moose to a tree, one is a tree and one is a animal.. programming is logic and understanding how that logi is similar to our everyday logic – Mikael Puusaari Jul 15 '17 at 06:45
1

You can use .Take(int) that takes just a certain part of the array. For example: myArray.Take(50) will take the first 50 elements of the array myArray.

After taking the first 50 elements, if you want to take the next 50 elements, you need to add an offset using the .Skip(int) method that will skip a certain number of elements in the array. For example, myArray.Skip(20).Take(50) will return the elements with index 20 to 49 of the array myArray.

Therefore, if you want to split an array of for equals part of 50 characters you need to to this:

char[] part1 = myArray.Take(50).ToArray(); // Elements 0 to 49
char[] part2 = myArray.Skip(50).Take(50).ToArray(); // Elements 50 to 99
char[] part3 = myArray.Skip(100).Take(50).ToArray(); // Elements 100 to 149
char[] part4 = myArray.Skip(150).Take(50).ToArray(); // Elements 150 to 199

Split() and Take() are Linq commands and change the array into an IEnumerable. The ToArray() after the Take(50) is to put it back in an array format. For more info about Linq: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/introduction-to-linq-queries

Maxime
  • 2,192
  • 1
  • 18
  • 23
  • This seem to work. But when I tried to test it with "interview.Job.Summary.Take(50).ToString()" to get the first 50 characters, it gave me "System.Linq.Enumerable+d__24`1[System.Char]". What seems to be the problem? – Dukakus17 Jul 15 '17 at 04:34
  • Add `ToArray()` after the `Take(50)` to put it back in an array format. Like this: `Take(50).ToArray().ToString()`. This problem is caused by the fact that `Split()` and `Take()` are Linq commands and change the array into an `IEnumerable`. For more info about Linq: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/introduction-to-linq-queries – Maxime Jul 15 '17 at 04:36
  • Now "interview.Job.Summary.Take(50).ToArray().ToString()" this gives me "System.Char[]". Is it not possible to do it this way? – Dukakus17 Jul 15 '17 at 04:50
  • `interview.Job.Summary.Take(50).ToArray()` returns an array if chars. After that it depends on what you want to do with it. If you want to turn it back to a string you can do `new String(interview.Job.Summary.Take(50).ToArray())` and it will create a new string with the chars of the array. – Maxime Jul 15 '17 at 04:55
  • 1
    "There is no way to just split an array in 4 parts of the same length in one function call. C# does not provide anything for that." - Yes there is. – Enigmativity Jul 15 '17 at 13:01
  • @Enigmativity what is it? – Maxime Jul 16 '17 at 03:32
  • @Enigmativity fixed my answer – Maxime Jul 16 '17 at 14:54