请把字符串A2B4C1D5E11转换成AABBBBCDDDDDEEEEEEEEEEE!

zhougonghenbang 2009-07-23 09:08:23
请把字符串A2B4C1D5E11转换成AABBBBCDDDDDEEEEEEEEEEE!

c#实现.简洁的写法!
...全文
267 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
zming 2009-07-23
  • 打赏
  • 举报
回复
学习了不少:
(1) new string(char c, int count)
(2) MatchEvaluator 委托

重写代码如下:


string msg = "A2B4C1D5E11";
string ret = ConvertString(msg);

public string ConvertString(string input)
{
string pattern = @"\p{L}\d+"; // 或 @"[a-zA-Z]\d+"
Regex regex = new Regex(pattern);
MatchHandler handler = new MatchHandler();
MatchEvaluator evaluator = new MatchEvaluator(handler.Replace);

return regex.Replace(input, evaluator);
}

public class MatchHandler
{
public string Replace(Match match)
{
char letter = match.Value[0];
int count = Convert.ToInt32(match.Value.Substring(1));

return new string(letter, count);
}
}
ljhcy99 2009-07-23
  • 打赏
  • 举报
回复
string s = "A2B4C1D5E11";
char[] charry = s.ToCharArray();
char p = '0';
int num =0;
StringBuilder b = new StringBuilder();

foreach (char c in charry)
{
if (Char.IsLetter(c))
{
if (num == 0)
{
p = c;
}
else
{
string ss = new string(p, num);
b.Append(ss);
num = 0;
p = c;

}
}
else
if (Char.IsDigit(c))
{
if (num != 0)
{
int l = num.ToString().Length;
string s1 = "1";
string s2 = new string('0', l);
s2 = s1 + s2;
num = num + (int)Char.GetNumericValue(c) * Convert.ToInt32(s2);
}
else
{
num = (int)Char.GetNumericValue(c);
}
}
}
if (num != 0)
{
string ss = new string(p, num);
b.Append(ss);

}
MessageBox.Show(b.ToString());
cailee 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zming 的回复:]
C# codestring msg="A2B4C1D5E11";string ret= ConvertString(msg);publicstring ConvertString(string input)
{string pattern=@"\p{L}\d+";// 或 @"[a-zA-Z]\d+" Regex regex=new Regex(pattern);
¡­
[/Quote]
good
mindstrong 2009-07-23
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string sInput, sRegex;
sInput = "A3B4E11";
// A very simple regular expression.
sRegex = "[a-zA-Z]{1}[1-9]?[0-9]*";
Regex r = new Regex(sRegex);
MyClass c = new MyClass();.
MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);
Console.WriteLine(sInput);
sInput = r.Replace(sInput, myEvaluator);
Console.WriteLine(sInput);
}

public string ReplaceCC(Match m)
{
string strTemp = m.ToString();
string ostr = strTemp[0].ToString();
string strnewTemp = "[" + strTemp[0] + "]{"+strTemp.Substring(1,strTemp.Length-1)+"}";
Regex or = new Regex(strnewTemp);
while(!or.IsMatch(ostr))
{
ostr+=strTemp[0].ToString();
Console.WriteLine(ostr);
}
return ostr;
}
thycloud 2009-07-23
  • 打赏
  • 举报
回复
3 楼 4 楼 当后面数字是 11 ,111 或者 1111111 的时候你那么处理肯定出错
数字不定是一位
wdzr_826 2009-07-23
  • 打赏
  • 举报
回复
string rebuildStr(string _Value)
{
char[] _ValueList = _Value.ToCharArray();
int flag = 0;
int dig = 0;
char cha = Char.MinValue;
string finalStr = "";
for (int i = 0; i < _ValueList.Length; i++)
{
if (Char.IsDigit(_ValueList[i]))
{
dig = dig * 10 + int.Parse(_ValueList[i].ToString());
if (i + 1 < _ValueList.Length && Char.IsLetter(_ValueList[i + 1]))
{
flag = 1;
}
else if (i + 1 == _ValueList.Length)
{
flag = 1;
}
}
else
{
cha = _ValueList[i];
}
if (flag == 1)
{
finalStr += new string(cha, dig);
flag = 0;
dig = 0;
cha = Char.MinValue;
}
}
return finalStr;
}
zming 2009-07-23
  • 打赏
  • 举报
回复

string msg = "A2B4C1D5E11";
string ret = ConvertString(msg);

public string ConvertString(string input)
{
string pattern = @"\p{L}\d+"; // 或 @"[a-zA-Z]\d+"
Regex regex = new Regex(pattern);
List<LetterCount> list = new List<LetterCount>();
StringBuilder builder = new StringBuilder();
for (Match match = regex.Match(input); match.Success; match = match.NextMatch())
{
LetterCount let = new LetterCount(match.Value);
builder.Append(let.ToString());
}

return builder.ToString();
}

public struct LetterCount
{
public char Letter;
public int Count;

public LetterCount(string val)
{
Letter = val[0];
Count = Convert.ToInt32(val.Substring(1));
}

public string ToString()
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < Count; i++)
{
builder.Append(Letter);
}
return builder.ToString();
}
}
qqiuzaihui 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wowoj2ee 的回复:]
StringBuilder sb = new StringBuilder();
for(int index = 0; index < str.Length; index += 2)
{
    char subStr = str[index];
    int num = Convert.ToInt32(str.Substring(index + 1, 1);
    for(int jndex = 0; jndex < num; jndex++)
    {
      sb.Append(subStr);
    }
}
UP, 与我的想法一致。
[/Quote]
wowoj2ee 2009-07-23
  • 打赏
  • 举报
回复
StringBuilder sb = new StringBuilder();
for(int index = 0; index < str.Length; index += 2)
{
char subStr = str[index];
int num = Convert.ToInt32(str.Substring(index + 1, 1);
for(int jndex = 0; jndex < num; jndex++)
{
sb.Append(subStr);
}
}
thycloud 2009-07-23
  • 打赏
  • 举报
回复
截取第一个 字母
再截取后面的字符串 1-N 循环直到不是数字
后面就简单了
yanm7788 2009-07-23
  • 打赏
  • 举报
回复
...........
zhougonghenbang 2009-07-23
  • 打赏
  • 举报
回复
长见识了...
zhougonghenbang 2009-07-23
  • 打赏
  • 举报
回复
楼上的都是高手....!
wang1wang2wang3 2009-07-23
  • 打赏
  • 举报
回复
好难啊~~~~~~~~~~~
LightColors 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 huwei001982 的回复:]
一句话搞定

C# codestring str= Regex.Replace("A2B4C1D5E11",@"(\w)(\d+)",delegate
(Match m)
{returnnewstring(m.Groups[1].Value[0], Convert.ToInt32(m.Groups[2].Value));
});
[/Quote]


这个。。。虽然一句话是好。。。

但是,。。。
xjhxyeti 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ojlovecd 的回复:]
C# codeusing System.Text.RegularExpressions;string str="A2B4C1D5E11";
StringBuilder sb=new StringBuilder();foreach (Match min Regex.Matches(str,@"([a-zA-Z])(\d+)"))
sb.Appe¡­
[/Quote]

顶。。。。。。。。。。。。。。。。。。。。。
rocker0325 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 huwei001982 的回复:]
一句话搞定

C# codestring str= Regex.Replace("A2B4C1D5E11",@"(\w)(\d+)",delegate
(Match m)
{returnnewstring(m.Groups[1].Value[0], Convert.ToInt32(m.Groups[2].Value));
});
[/Quote]

顶。。。。。。。。
lovelan1748 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ojlovecd 的回复:]
C# codeusing System.Text.RegularExpressions;string str="A2B4C1D5E11";
StringBuilder sb=new StringBuilder();foreach (Match min Regex.Matches(str,@"([a-zA-Z])(\d+)"))
sb.Append(newstring(m.Groups[1].Value[0],int.Parse(m.Groups[2].Value)));string result= sb.ToString();
[/Quote]
学习了,谁能讲解下正则常用的符号啊
huwei001982 2009-07-23
  • 打赏
  • 举报
回复
一句话搞定

            string str = Regex.Replace("A2B4C1D5E11", @"(\w)(\d+)", delegate
(Match m)
{
return new string(m.Groups[1].Value[0], Convert.ToInt32(m.Groups[2].Value));
});
lovelan1748 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 thycloud 的回复:]
3 楼 4 楼 当后面数字是 11 ,111 或者 1111111 的时候你那么处理肯定出错
数字不定是一位
[/Quote]
加载更多回复(5)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧