Results 1 to 5 of 5

Thread: [RESOLVED] didn't replace the charater using .replace()

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    14

    Resolved [RESOLVED] didn't replace the charater using .replace()

    here's the code:
    Code:
    If RichTextBox4.Text.Contains(vbLf) Then
          RichTextBox4.Text.Replace(vbLf, "")
    End If
    I tried to add a break-point in it and shows it's running properly. But the results still have vbLf char in it.
    how can i solve it?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: didn't replace the charater using .replace()

    The Replace method is one of those where you need to assign the result, eg:
    Code:
    If RichTextBox4.Text.Contains(vbLf) Then
          RichTextBox4.Text = RichTextBox4.Text.Replace(vbLf, "")
    End If
    Note however that as this is modifying the .Text property, any formatting will be lost.


    Also note that this will only remove LF characters, any CR characters (which have almost the same effect as LF) will still be there.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: didn't replace the charater using .replace()

    The documentation for the String.Replace method says this:
    This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldValue are replaced by newValue.
    That's why you should always read the relevant documentation if something doesn't seem to work the way you expect.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: didn't replace the charater using .replace()

    This is true of basically anything related to Strings in .NET. They are "immutable reference types" which means they sometimes behave more like structs, even though they're not structs. The "immutable" part means once you create a string, you can't change it. You can only create a new string. So the Replace() function builds the new string and returns it. It can't do anything to the string it's called on.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    14

    Re: didn't replace the charater using .replace()

    thanks. now i can using the value properly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width