C# 用整数替换####
通过使用RegEx或String.Replace,我需要将任意数量的连续#替换为整数(使用适当数量的前导0)。我知道我可以在字符串中搜索#,得到第一个和最后一个索引,最后一个是长度,然后替换为string.replace。我希望有人能有一个更快、更圆滑的答案 方法头将是:C# 用整数替换####,c#,C#,通过使用RegEx或String.Replace,我需要将任意数量的连续#替换为整数(使用适当数量的前导0)。我知道我可以在字符串中搜索#,得到第一个和最后一个索引,最后一个是长度,然后替换为string.replace。我希望有人能有一个更快、更圆滑的答案 方法头将是: string ReplaceHashtagsWithInt(string input, int integer) 示例: 输入->“字符串#############”,2 输出->“String0002\u Hi02” 输入
string ReplaceHashtagsWithInt(string input, int integer)
示例:
输入->“字符串#############”,2
输出->“String0002\u Hi02”
输入->“字符串############”,123
输出->“String0123\u Hi123”公共静态类测试
{
公共静态void Main()
{
替换hashtagswithint(“String###########Hi##”,2);
替换hashtagswithint(“String############Hi##,”,123);
替换hashtagswithint(“String#################”123);
}
公共静态字符串ReplaceHashtagsWithInt(字符串输入,整数)
{
正则表达式正则表达式=新正则表达式(“#+”);
var matches=regex.matches(input.Cast().Select(m=>m.Value.ToArray();
数组。排序(匹配项);
数组。反向(匹配);
foreach(匹配中的字符串匹配)
{
正则表达式r=新正则表达式(匹配);
字符串零=新字符串('0',match.Length-integer.ToString().Length)+整数;
输入=r.替换(输入,零);
}
返回输入;
}
}
公共静态类测试
{
公共静态void Main()
{
替换hashtagswithint(“String###########Hi##”,2);
替换hashtagswithint(“String############Hi##,”,123);
替换hashtagswithint(“String#################”123);
}
公共静态字符串ReplaceHashtagsWithInt(字符串输入,整数)
{
正则表达式正则表达式=新正则表达式(“#+”);
var matches=regex.matches(input.Cast().Select(m=>m.Value.ToArray();
数组。排序(匹配项);
数组。反向(匹配);
foreach(匹配中的字符串匹配)
{
正则表达式r=新正则表达式(匹配);
字符串零=新字符串('0',match.Length-integer.ToString().Length)+整数;
输入=r.替换(输入,零);
}
返回输入;
}
}
您可以执行以下操作:
using System;
using System.Text;
using System.Text.RegularExpressions;
public static class Testing
{
public static void Main()
{
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 1));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 23));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 456));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 7890));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 78901));
}
public static string ReplaceHashtagsWithInt(string input, int integer)
{
Regex regex = new Regex("#+");
StringBuilder output = new StringBuilder(input);
int allig = 0;
for(Match match = regex.Match(input);match.Success;match = match.NextMatch())
{
string num = integer.ToString();
if(num.Length<=match.Length)
for(int i=0;i<match.Length;i++)
{
if(i<match.Length-num.Length)
output[match.Index+i+allig] = '0';
else
output[match.Index+i+allig] = num[i-match.Length+num.Length];
}
else
{
output.Remove(match.Index+allig,match.Length);
output.Insert(match.Index+allig,num);
allig+=num.Length-match.Length;
}
}
return output.ToString();
}
}
使用系统;
使用系统文本;
使用System.Text.RegularExpressions;
公共静态类测试
{
公共静态void Main()
{
Console.WriteLine(替换hashtagswithint(“############,1));
Console.WriteLine(替换hashtagswithint(“############,23));
Console.WriteLine(替换HashTagsWithint(“##############,”,456));
Console.WriteLine(替换HashTagsWithint(“################,,7890));
Console.WriteLine(替换HashTagsWithint(“##############,”,78901));
}
公共静态字符串ReplaceHashtagsWithInt(字符串输入,整数)
{
正则表达式正则表达式=新正则表达式(“#+”);
StringBuilder输出=新的StringBuilder(输入);
int-allig=0;
for(Match=regex.Match(输入);Match.Success;Match=Match.NextMatch())
{
字符串num=integer.ToString();
如果(num.Length您可以执行以下操作:
using System;
using System.Text;
using System.Text.RegularExpressions;
public static class Testing
{
public static void Main()
{
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 1));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 23));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 456));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 7890));
Console.WriteLine(ReplaceHashtagsWithInt("###_####_#", 78901));
}
public static string ReplaceHashtagsWithInt(string input, int integer)
{
Regex regex = new Regex("#+");
StringBuilder output = new StringBuilder(input);
int allig = 0;
for(Match match = regex.Match(input);match.Success;match = match.NextMatch())
{
string num = integer.ToString();
if(num.Length<=match.Length)
for(int i=0;i<match.Length;i++)
{
if(i<match.Length-num.Length)
output[match.Index+i+allig] = '0';
else
output[match.Index+i+allig] = num[i-match.Length+num.Length];
}
else
{
output.Remove(match.Index+allig,match.Length);
output.Insert(match.Index+allig,num);
allig+=num.Length-match.Length;
}
}
return output.ToString();
}
}
使用系统;
使用系统文本;
使用System.Text.RegularExpressions;
公共静态类测试
{
公共静态void Main()
{
Console.WriteLine(替换hashtagswithint(“############,1));
Console.WriteLine(替换hashtagswithint(“############,23));
Console.WriteLine(替换HashTagsWithint(“##############,”,456));
Console.WriteLine(替换HashTagsWithint(“################,,7890));
Console.WriteLine(替换HashTagsWithint(“##############,”,78901));
}
公共静态字符串ReplaceHashtagsWithInt(字符串输入,整数)
{
正则表达式正则表达式=新正则表达式(“#+”);
StringBuilder输出=新的StringBuilder(输入);
int-allig=0;
for(Match=regex.Match(输入);Match.Success;Match=Match.NextMatch())
{
字符串num=integer.ToString();
如果(num.length)您可以发布您尝试过的代码,以及它似乎无法按预期工作的地方吗?在字符串中搜索“#”可能是最快的方法。利用string.Format(),您将希望用{0:000}替换运行的#####,确保0的数量与#的数量匹配。需要大约8行代码。您可以发布您尝试过的代码,以及它似乎无法按预期工作的地方吗?在字符串中搜索“#”可能是最快的方法。利用string.Format(),您将希望用{0:000}替换一次###,确保0的数量与##的数量匹配。需要大约8行代码。如果它在较大的组之前找到一个较小的组,则此操作将失败-因此“##-#####-#2
将成为002-002
@Aravol,nice catch.changer.Replace(输入,零)
r.Replace(输入,零,1,匹配索引)
并在match.Length-integer.ToString()时进行保护。Length
<0以修复您的解决方案如果它在较大的组之前找到较小的组,则此解决方案将失败-因此“##-######-######2
将变成002-002#
@Aravol,漂亮的捕获。更改r.Replace(输入,零,1,match.Index);
并在match.Length-integer.ToString().Length
<0时进行保护以修复您的解决方案