0

Does anybody know I can replace a group of numbers in a string by one *. For example if I have a string like this "Test123456.txt", I want to convert it to "Test#.txt". I have seen plenty of examples that can replace each individual number with a new character, but none that deal with a group of numbers. Any help is much appreciated!

0

3 Answers 3

5
Regex r = new Regex(@"\d+", RegexOptions.None);
            Console.WriteLine(r.Replace("Test123456.txt", "#"));
            Console.Read();
Sign up to request clarification or add additional context in comments.

Comments

1

Use Regex.Replace() as follows:

string fileName = "Test12345.txt";
string newFileName = Regex.Replace(fileName, @"[\d]+", "#");

Comments

1

you can use regex, to do this, but if you know the exact text, then using the string.Replace method would be more efficient:

string str =  "blahblahblahTest123456.txt";
str = string.Replace("Test#.txt","Test123456.txt");

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.