-
December 5th, 2007, 03:53 AM
#1
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??
-
December 5th, 2007, 04:12 AM
#2
Re: Replace. to _ in a string
Code:
string myString = "This is a sentence.";
myString.Replace('.', '_');
Or do you really want to use regex?
-
December 5th, 2007, 04:17 AM
#3
Re: Replace. to _ in a string
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?
-
December 5th, 2007, 04:53 AM
#4
Re: Replace. to _ in a string
Originally Posted by lsy
any other function able to provide this?
What's wrong with using String.Replace( )?
-
December 5th, 2007, 05:18 AM
#5
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.
-
December 5th, 2007, 06:13 AM
#6
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: "\\."
-
December 5th, 2007, 08:17 PM
#7
Re: Replace. to _ in a string
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|