String.Remove/String.Replace ain't working?

Hi everyone!
Could somebody tell me why can't I remove parts from my string?

Resuming: Basically I have these string _data which goes to my database and gave me that value.

string _data = "2019/1/13 00:00:00"

_data.Remove(10, 8);
_data.Remove(10);
_data.Replace(" 00:00:00", "");

I already tried to use all of this methods but, for some reason, they are not working. What am i doing wrong?

P.S: I am doing this inside a foreach

Best Answer

  • LucasZhangLucasZhang Xamurai
    Accepted Answer

    Because String.Remove method returns a new string. It doesn't change original one.

    string _data = "2019/1/13 00:00:00"
    
    string _newData =_data.Remove(10, 8);
    

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    Accepted Answer

    Because String.Remove method returns a new string. It doesn't change original one.

    string _data = "2019/1/13 00:00:00"
    
    string _newData =_data.Remove(10, 8);
    
  • ricardoventuraricardoventura Member ✭✭

    @LucasZhang said:
    Because String.Remove method returns a new string. It doesn't change original one.

    string _data = "2019/1/13 00:00:00"
    
    string _newData =_data.Remove(10, 8);
    

    Ohh woaw. Didn't think about having a new string.... thanks for your replying! That helped! Much love

  • JohnHardmanJohnHardman GBUniversity mod

    @ricardoventura said:
    Ohh woaw. Didn't think about having a new string.... thanks for your replying! That helped! Much love

    Strings are immutable in C#, meaning that you cannot change them. Any operation on a string will create a new string. Of course, you can then assign that string to the same variable as the original one, hence having the variable now contain a reference to the new string.

    string _data = "2019/1/13 00:00:00"
    _data =_data.Remove(10, 8);
    
Sign In or Register to comment.