0

I am formatting file which is downloaded from internet and I encountered ... , ...... So I want to replace these multiple dots with single dot. String.Replace is not going work here and I am not good with regular expressions So what is the solution..?

Thank you

2
  • So in your input string of ... , .... you'd want . , . to be the result? Commented Jun 21, 2011 at 15:03
  • yes I want to replace multiple dots with single dot Commented Jun 21, 2011 at 15:04

2 Answers 2

5

My guess is you could use Regex.Replace:

text = Regex.Replace(text, @"\.+", ".");

Note that you don't have to escape the . in the replacement pattern - only substitution patterns are recognised within the replacement pattern.

Sign up to request clarification or add additional context in comments.

7 Comments

yeh.. The above code works. But why did you used "@" here...?
@AjitHegde: It's a case of a string literal. When you need (or want) to use escaped characters in a string and not escape the string itself (i.e., regex, file paths, etc.) you use these for readability/ease of use.
The "@" causes the C# compiler to treat the '\' as a normal character instead as an escape character
I used it with files but I didnt know It also works with regular expressions.. Thanks for the information
@Brad: I think you mean a verbatim string literal. Just "foo" is a regular string literal.
|
0
myFileContents = Regex.Replace(myFileContents, @"\.+", ".");

[Reference]

In the future, please reference this site as a source of help, not code. That is to say, provide what you've tried, what you're having problem(s) with, and how it's not working as you've anticipated--not "code this for me".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.