C# 重复子字符串N次
我收到一系列后跟非负数的字符串,例如C# 重复子字符串N次,c#,regex,C#,Regex,我收到一系列后跟非负数的字符串,例如“a3”。我必须在控制台上打印每个重复N次(大写)的字符串,其中N是输入中的一个数字。在本例中,结果是:“AAA”。正如你所看到的,我试着从输入中获取数字,我认为它工作得很好。你能帮我复习一下吗 string input = Console.ReadLine(); //input = "aSd2&5s@1" MatchCollection matched = Regex.Matches(input, @"\d+"); List<int> r
“a3”
。我必须在控制台上打印每个重复N
次(大写)的字符串,其中N
是输入中的一个数字。在本例中,结果是:“AAA”
。正如你所看到的,我试着从输入中获取数字,我认为它工作得很好。你能帮我复习一下吗
string input = Console.ReadLine();
//input = "aSd2&5s@1"
MatchCollection matched = Regex.Matches(input, @"\d+");
List<int> repeatsCount = new List<int>();
foreach (Match match in matched)
{
int repeatCount = int.Parse(match.Value);
repeatsCount.Add(repeatCount);
}
//repeatsCount: [2, 5, 1]
//expected output: ASDASD&&&&&S@ ("aSd" is converted to "ASD" and repeated twice;
// "&" is repeated 5 times; "s@" is converted to "S@" and repeated once.)
string input=Console.ReadLine();
//input=“aSd2&5s@1"
MatchCollection matched=Regex.Matches(输入@“\d+”);
List RepeatScont=新列表();
foreach(匹配中匹配)
{
int repeatCount=int.Parse(match.Value);
RepeatScont.Add(repeatCount);
}
//重复提示:[2,5,1]
//预期输出:ASDASD&S@(“aSd”)转换为“aSd”并重复两次;
//“&”重复5次;“s@”转换为“s@”并重复一次。)
例如,如果我们有“aSd2和5s@1”:
“aSd”
转换为“aSd”
并重复两次<代码>“&”重复5次<代码>“s@”转换为“s@”
并重复一次让模式包括两组:要重复的值
和要重复的次数:
@"(?<value>[^0-9]+)(?<times>[0-9]+)"
编辑:没有Linq的相同想法:
StringBuilder sb=新建StringBuilder();
foreach(Regex.Matches中的Match(源,@“(?[^0-9]+)(?[0-9]+)”)匹配){
字符串值=match.Groups[“value”].value.ToUpper();
int times=int.Parse(match.Groups[“times”].Value);
对于(int i=0;i
您可以提取子字符串,以及使用此正则表达式重复子字符串的频率:
(?<content>.+?)(?<count>\d+)
输出为
ASDASD&S@
没有LINQ的另一个解决方案 我试图保留该解决方案,使其与您的类似
string input = "aSd2&5s@1";
var matched = Regex.Matches(input, @"\d+");
var builder = new StringBuilder();
foreach (Match match in matched)
{
string stingToDuplicate = input.Split(Char.Parse(match.Value))[0];
input = input.Replace(stingToDuplicate, String.Empty).Replace(match.Value, String.Empty);
for (int i = 0; i < Convert.ToInt32(match.Value); i++)
{
builder.Append(stingToDuplicate.ToUpper());
}
}
string input=“aSd2&5s@1";
var matched=Regex.Matches(输入@“\d+”);
var builder=新的StringBuilder();
foreach(匹配中匹配)
{
string stingToDuplicate=input.Split(Char.Parse(match.Value))[0];
input=input.Replace(stingToDuplicate,String.Empty)。Replace(match.Value,String.Empty);
for(int i=0;i
最后是Console.WriteLine(builder.ToString())代码>
哪个结果ASDASD&&&&S@
我的解决方案与其他解决方案相同,但略有不同:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
string input = "aSd2&5s@1";
string pattern1 = @"[a-zA-z@&]+\d+";
MatchCollection matches = Regex.Matches(input, pattern1);
string output = "";
foreach(Match match in matches.Cast<Match>().ToList())
{
string pattern2 = @"(?'string'[^\d]+)(?'number'\d+)";
Match match2 = Regex.Match(match.Value, pattern2);
int number = int.Parse(match2.Groups["number"].Value);
string str = match2.Groups["string"].Value;
output += string.Join("",Enumerable.Repeat(str.ToUpper(), number));
}
Console.WriteLine(output);
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序107
{
班级计划
{
静态void Main(字符串[]参数)
{
string input=“aSd2&5s@1";
字符串模式1=@“[a-zA-z@&]+\d+”;
MatchCollection matches=Regex.matches(输入,pattern1);
字符串输出=”;
foreach(matches.Cast().ToList()中的匹配)
{
字符串模式2=@“(?'string'[^\d]+)(?'number'\d+);
Match match2=Regex.Match(Match.Value,pattern2);
int number=int.Parse(match2.Groups[“number”].Value);
string str=match2.Groups[“string”].Value;
output+=string.Join(“,Enumerable.Repeat(str.ToUpper(),number));
}
控制台写入线(输出);
Console.ReadLine();
}
}
}
非常简单的程序。无linq nothing,简单的字符串
和for
循环
string input = "aSd2&5s@1";
char[] inputArray = input.ToCharArray();
string output = "";
string ab = "";
foreach (char c in inputArray)
{
int x;
string y;
if(int.TryParse(c.ToString(), out x))
{
string sb = "";
ab = ab.ToUpper();
for(int i=0;i<b;i++)
{
sb += ab;
}
ab = "";
output += sb;
}
else
{
ab += c;
}
}
if(!string.IsNullOrEmpty(ab))
{
output += ab.ToUpper();
}
Console.WriteLine(output);
string input=“aSd2&5s@1";
char[]inputArray=input.ToCharArray();
字符串输出=”;
字符串ab=“”;
foreach(输入阵列中的字符c)
{
int x;
弦y;
if(int.TryParse(c.ToString(),out x))
{
串某人“”;
ab=ab.ToUpper();
因为(int i=0;我自己做不到,这就是我在这里发帖的原因。如果这是家庭作业问题,我会和我的老师联系。好吧,如果没有LINQ的文本和。First()
的代码,它看起来很奇怪:)@AleksAndreev我看不到。First()
我的代码中的任何地方,你知道吗?:)现在我不知道了。但它就在那里。现在你删除了它,所以我不再向你提问了
var input = "aSd2&5s@1";
var regex = new Regex("(?<content>.+?)(?<count>\\d+)");
var matches = regex.Matches(input).Cast<Match>();
var sb = new StringBuilder();
foreach (var match in matches)
{
var count = int.Parse(match.Groups["count"].Value);
for (var i = 0; i < count; ++i)
sb.Append(match.Groups["content"].Value.ToUpper());
}
Console.WriteLine(sb.ToString());
string input = "aSd2&5s@1";
var matched = Regex.Matches(input, @"\d+");
var builder = new StringBuilder();
foreach (Match match in matched)
{
string stingToDuplicate = input.Split(Char.Parse(match.Value))[0];
input = input.Replace(stingToDuplicate, String.Empty).Replace(match.Value, String.Empty);
for (int i = 0; i < Convert.ToInt32(match.Value); i++)
{
builder.Append(stingToDuplicate.ToUpper());
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
string input = "aSd2&5s@1";
string pattern1 = @"[a-zA-z@&]+\d+";
MatchCollection matches = Regex.Matches(input, pattern1);
string output = "";
foreach(Match match in matches.Cast<Match>().ToList())
{
string pattern2 = @"(?'string'[^\d]+)(?'number'\d+)";
Match match2 = Regex.Match(match.Value, pattern2);
int number = int.Parse(match2.Groups["number"].Value);
string str = match2.Groups["string"].Value;
output += string.Join("",Enumerable.Repeat(str.ToUpper(), number));
}
Console.WriteLine(output);
Console.ReadLine();
}
}
}
string input = "aSd2&5s@1";
char[] inputArray = input.ToCharArray();
string output = "";
string ab = "";
foreach (char c in inputArray)
{
int x;
string y;
if(int.TryParse(c.ToString(), out x))
{
string sb = "";
ab = ab.ToUpper();
for(int i=0;i<b;i++)
{
sb += ab;
}
ab = "";
output += sb;
}
else
{
ab += c;
}
}
if(!string.IsNullOrEmpty(ab))
{
output += ab.ToUpper();
}
Console.WriteLine(output);