locked
How to Append new line in each element of string array RRS feed

  • Question

  • User339833461 posted

    Hello Everyone, 

    I am facing bit problem with string array. pls help me here

    Actually i have a string like below

    string[] str1;
    str1 = new string[5]{ Element 1”, Element 2”, Element 3”, Element 4”, Element 5 };

    Here i want to check each element in string array has new line or not. If it's not having new line append to it, i need to append new line like below

    str1 = new string[5]{ Element 1\n”, Element 2\n”, Element 3\n”, Element 4\n”, Element 5\n };

    How to achieve this. 

    Pls help me

    Thanks,

    Thursday, February 27, 2020 7:24 AM

All replies

  • User288213138 posted

    Hi LR,

    Here i want to check each element in string array has new line or not. If it's not having new line append to it, i need to append new line like below

    You can use a for loop to iterate through your array and then use the EndsWith method to check if the elements have new line or not.

    string[] str1=new string[5] { "Element 1", "Element 2", "Element 3", "Element 4", "Element 5" };
             
                for (int i = 0; i < str1.Length; i++)
                {
                    if (!str1[i].EndsWith("\n"))
                    {
                        str1[i] += "\n";
                    }
                }

    Best regards,

    Sam

    Thursday, February 27, 2020 9:40 AM