none
How to remove last string element unnecessary text if exists? RRS feed

  • Question

  • Hello All, 

    I have a string array with 3 elements like below for example.

    string[] stringarray1;
    
    stringarray1 = new string[5]{ “Element 1\n”, “Element 2\n”, “Element 3\n”, “Element 4\n”, “Element 5\nblablablabla” };
    Here i need to check last element in string array having unnecessary dynamic text "\nblablablabla", if exists i need to remove(till last of the dynamic text) and replace with "Element 5\n" in last element. 

    How to achive it. pls help me

    Thanks


    Monday, March 2, 2020 7:34 AM

Answers

  • Actually if you're using LINQ, you can use the fact that non-null string on String.Split() will always result in array of at least one element to do the following:

                var source = new string[5] { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
                source = source.Select(x => x.Split('\n')[0] + '\n').ToArray();
    without knowing the exact string to remove.




    Monday, March 2, 2020 8:58 AM
    Answerer

All replies

  • So it is just the last entry of the array you want to check? How do you identify the dynamic unnecessary text? Perhaps this will help:

    // use LINQ on all elements
    var source = new string[5] { @"Element 1\n", @"Element 2\n", @"Element 3\n", @"Element 4\n", @"Element 5\nblablablabla" };
    var invalid = "blablablabla";
    source = source.Select(elem => elem.Replace(invalid, String.Empty)).ToArray();

    or witout LINQ

    var source = new string[5] { @"Element 1\n", @"Element 2\n", @"Element 3\n", @"Element 4\n", @"Element 5\nblablablabla" };
    var invalid = "blablablabla";
    for (var i = 0; i < source.Length; i++)
    {
        source[i] = source[i].Replace(invalid, String.Empty);
    }

    or only check the last element in the array:

    source[source.Length - 1] = source[source.Length - 1].Substring(0, source[source.Length - 1].Length - invalid.Length);


    Monday, March 2, 2020 8:53 AM
  • Actually if you're using LINQ, you can use the fact that non-null string on String.Split() will always result in array of at least one element to do the following:

                var source = new string[5] { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
                source = source.Select(x => x.Split('\n')[0] + '\n').ToArray();
    without knowing the exact string to remove.




    Monday, March 2, 2020 8:58 AM
    Answerer
  • Hi Learning Rocks.

    Thank you for posting here.

    Now that you know that the last element may have an incorrect value, and you know that the correct value is "Element 5 \ n", isn't it possible to assign directly?

                if (stringarray1[stringarray1.Length - 1] != "Element 5\n")
                {
                    stringarray1[stringarray1.Length - 1] = "Element 5\n";
                }

    Are there other conditions you did not specify?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 9:01 AM