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

  • Question

  • User339833461 posted

    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:35 AM

Answers

  • User1535942433 posted

    Hi Learning Rocks,

    Accroding to your description,I suggest you could find the last element and check whether the string is you need.If the string have unnecessary  something,you could replace with "Element 5\n".

    More details,you could refer to below codes:

    string[] stringarray1 = { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
                string x = stringarray1.Last();
                if (x != "Element 5\n")
                {
                    x = stringarray1[stringarray1.Length - 1].Replace(x, "Element 5\n");
                }
    
                Console.WriteLine(x);

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2020 7:11 AM

All replies

  • User-37275327 posted
    var stringarray1 = new string[5]{ "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
    var x = stringarray1[stringarray1.Length - 1].Replace("blablablabla","");
    		

    Monday, March 2, 2020 8:45 AM
  • User-1605831695 posted

    Hi @learing Rocks,

            string[] stringarray1;
            
            stringarray1 = new string[5] { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
    
            string[] items2 = stringarray1.Select(x => x.Replace("blablablabla", "")).ToArray();
            
            foreach (var item in items2)
            {
                Response.Write(item);
            }
    

    can you please try this.. it is tested code.

    Tuesday, March 3, 2020 6:55 AM
  • User1535942433 posted

    Hi Learning Rocks,

    Accroding to your description,I suggest you could find the last element and check whether the string is you need.If the string have unnecessary  something,you could replace with "Element 5\n".

    More details,you could refer to below codes:

    string[] stringarray1 = { "Element 1\n", "Element 2\n", "Element 3\n", "Element 4\n", "Element 5\nblablablabla" };
                string x = stringarray1.Last();
                if (x != "Element 5\n")
                {
                    x = stringarray1[stringarray1.Length - 1].Replace(x, "Element 5\n");
                }
    
                Console.WriteLine(x);

    Result:

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2020 7:11 AM