none
Help when Splitting a String

    Question

  • What i am trying to do here is copy all this text to the clipboard then to a string manipulation to format this into just  two entries. In C# i am trying to use String.Split('\n') to split these lines. As a result, it creates an array having a length of 6 cause one array/line. Do you guys know how can i manipulate this array and combine it into two array only of if there is a parameter i can pass to the Split method to just split the entries every after 3 rows.

    Regards,

    Denden

    Tuesday, May 2, 2017 3:08 PM

Answers

  • You can't split every X lines, but it's not difficult to Join ... do something like this:

    string[] splitArray = { "one", "two","three","four","five","six" };
    List<string> combined = new List<string>();
    for (int i = 0; i < splitArray.Length - 3; i=i+3)
    {
        combined.Add(string.Join(" ", splitArray, i, 3));
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Dikong42 Saturday, May 6, 2017 6:41 PM
    Tuesday, May 2, 2017 4:21 PM
    Moderator

All replies

  • Tuesday, May 2, 2017 3:26 PM
  • Can i just split every after three lines so i wont have hard time dealing with how to join them properly.
    Tuesday, May 2, 2017 4:04 PM
  • You can't split every X lines, but it's not difficult to Join ... do something like this:

    string[] splitArray = { "one", "two","three","four","five","six" };
    List<string> combined = new List<string>();
    for (int i = 0; i < splitArray.Length - 3; i=i+3)
    {
        combined.Add(string.Join(" ", splitArray, i, 3));
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Dikong42 Saturday, May 6, 2017 6:41 PM
    Tuesday, May 2, 2017 4:21 PM
    Moderator
  • Thank you sir. That is exactly im trying to do. I just cant get to make it work.
    Saturday, May 6, 2017 6:41 PM
  • Could you post the code you used? How is it not working?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, May 6, 2017 8:11 PM
    Moderator
  • Edit: Thank you sir. That is exactly im trying to do. I just cant get to make it work <before>.

    What i mean by not working is my previous code. Your code works as i expected. Unluckily, my client wants to copy strings from an HTML table and not from an excel sheet anymore. The format was the same, but when i tried to see the output of the split method, it doesn't return 3 rows/lines anymore but more than that. I tried to paste the strings from an HTML table on Excel and the excel shows three rows which is what i expect. Do you have any idea sir?

    Here is the output of the clipboard when copying from an excel sheet.

    Meanwhile, this is the output when copying from an HTML table.

    • Edited by Dikong42 Thursday, May 11, 2017 5:57 AM
    Thursday, May 11, 2017 5:53 AM
  • Update: The problem is not with the code but the data when copying strings from an HTML table..

    Solved: 

    Thursday, May 11, 2017 1:53 PM
  • I don't know what to suggest, there are obviously going to be differences in the string because of the HTML. I don't know if there are tabs ( which would be \t ) that you need be aware of in addition to the \n. That may be part of the problem. Look at the string containing all that HTML and see what else you need to remove. Use some kind of text editor or text visualizer that will show unprintable characters (such as tabs). I use Notepad++ https://notepad-plus-plus.org/

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, May 11, 2017 1:53 PM
    Moderator
  • Thanks sir. I use this Copy Paste Extension to somehow formats the data. I will need to still manipulate it though but it definitely helped me to step forward. Thank you so much.
    Friday, May 12, 2017 5:34 AM
  • You're welcome! Glad I could help!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, May 13, 2017 6:26 AM
    Moderator