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!
Regex r = new Regex(@"\d+", RegexOptions.None);
Console.WriteLine(r.Replace("Test123456.txt", "#"));
Console.Read();
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");
Use Regex.Replace()
as follows:
string fileName = "Test12345.txt";
string newFileName = Regex.Replace(fileName, @"[\d]+", "#");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.