CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2005
    Posts
    568

    Replace. to _ in a string

    I'm try to replace "." to "_" in a string... i use Regex.Replace()... so my code is Regex.Replace("abc.123", ".", "_") but it return _______ to be...so can anyone help on this??

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Replace. to _ in a string

    Code:
    string myString = "This is a sentence.";
    myString.Replace('.', '_');
    Or do you really want to use regex?

  3. #3
    Join Date
    Feb 2005
    Posts
    568

    Re: Replace. to _ in a string

    Quote Originally Posted by Tischnoetentoet
    Code:
    string myString = "This is a sentence.";
    myString.Replace('.', '_');
    Or do you really want to use regex?
    any other function able to provide this?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Replace. to _ in a string

    Quote Originally Posted by lsy
    any other function able to provide this?
    What's wrong with using String.Replace( )?

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Replace. to _ in a string

    For something so basic, you'd be mad not to use string.Replace().
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: Replace. to _ in a string

    Use stringVariable.Replace(".", "_") since you don't seem to know what a regular expression is. stringVariable.Replace(".", "_") will return a new string with "." replaced with "_" in stringVariable, just like you wanted.

    With regular expressions, "." means "any character except newline and carriage return". That's why you only get underscores. Regular expressions support escaping, so preceed the dot with a backslash to bypass its special meaning. Since C# also uses backslashes for escaping, you need to escape the backslash as well: "\\."

  7. #7
    Join Date
    Feb 2005
    Posts
    568

    Re: Replace. to _ in a string

    Quote Originally Posted by andreasblixt
    Use stringVariable.Replace(".", "_") since you don't seem to know what a regular expression is. stringVariable.Replace(".", "_") will return a new string with "." replaced with "_" in stringVariable, just like you wanted.

    With regular expressions, "." means "any character except newline and carriage return". That's why you only get underscores. Regular expressions support escaping, so preceed the dot with a backslash to bypass its special meaning. Since C# also uses backslashes for escaping, you need to escape the backslash as well: "\\."
    Thanks for your explanation...

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

Featured