//Char.IsDigit(),Char.IsLetter() string ori = "A2B4C1D5E11" IList<char> list = new List<char>(); for(int i = 0; i < ori.Length-1; i++){ if(Char.IsLetter(ori[i])){ list.Add(ori[i]) }else{ if(Char.IsDigit(ori[i])){ //添加ori[i]个字符(list[list.Count-1]) } } } 没在编辑器里调试过,可能会有笔误
using System; using System.Collections.Generic; using System.Text;namespace ConsoleA { class Program { static void Main(string[] args) { string str = "A200B4C1D5E41"; string s;
getStr(ref str ,out s); Console.Write(s); Console.Read(); } //以out参数实现 public static void getStr(ref string str , out string outString) {
StringBuilder _Text = new StringBuilder(); char _Temp = ' ';
string _Numb = "";
char[] _ValueList = _Value.ToCharArray(); for(int i=0;i!=_ValueList.Length;i++)
{
if (_Value[i] > 57)
{
if (_Numb.Length != 0)
{
_Text.AppendLine(new string(_Temp, int.Parse(_Numb)));
_Numb = "";
}
_Temp = _Value[i];
}
else
{
_Numb += _Value[i].ToString();
}
}
if (_Numb.Length != 0) _Text.AppendLine(new string(_Temp, int.Parse(_Numb)));
这样?
或用Dictionary<string,string>
string sPattern = @"\w\d+";
string sResultValue = "";
MatchCollection mcs = Regex.Matches(s, sPattern, RegexOptions.IgnoreCase);
for (int i = 0; i < mcs.Count; i++)
{
string s1 = mcs[i].ToString();
string sValue = InterPret(s1);
sResultValue += sValue;//累加 最后的值为AABBBBCDDDDDEEEEEEEEEEE
} private string InterPret(string s)
{
string s1 = s.Substring(0, 1);
string s2 = s.Substring(1);
int count = Convert.ToInt32(s2);
string sRes = ""; for (int i = 0; i < count; i++)
{
sRes += s1;
} return sRes;
}
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;
}
List<int> list = new List<int>();
List<string> l = new List<string>();
int len=0;
string s=string.Empty;
while (len < firstS.Length)
{
if ((firstS[len] >= 48) && (firstS[len] <= 57))
{
s += firstS[len].ToString();
len++;
continue;
}
if (!string.IsNullOrEmpty(s))
{
list.Add(Convert.ToInt32(s));
}
l.Add(firstS[len].ToString());
s = string.Empty;
len++;
}
if (!string.IsNullOrEmpty(s))
{
list.Add(Convert.ToInt32(s));
} string newString="";
int length=0;
for (int j = 0; j < list.Count; j++)
{
length=0;
while (length <list[j])
{
newString +=l[j];
length++;
}
}
Response.Write(newString);
分割字符串,通过List <string>和list <int>保存字符和数字,再重新生成字符串
或用Dictionary <string,string>
string ori = "A2B4C1D5E11"
IList<char> list = new List<char>();
for(int i = 0; i < ori.Length-1; i++){
if(Char.IsLetter(ori[i])){
list.Add(ori[i])
}else{
if(Char.IsDigit(ori[i])){
//添加ori[i]个字符(list[list.Count-1])
}
}
}
没在编辑器里调试过,可能会有笔误
using System.Collections.Generic;
using System.Text;namespace ConsoleA
{
class Program
{
static void Main(string[] args)
{
string str = "A200B4C1D5E41"; string s;
getStr(ref str ,out s); Console.Write(s); Console.Read();
} //以out参数实现
public static void getStr(ref string str , out string outString) {
//在“模版”字符串中填充一个字符;
char[] charAry = (str + "a").ToCharArray(); //保存要附加的字符个数;
int numOfChar = 0;
//保存要附加的字符,
char currentChar = ' '; StringBuilder strBud = new StringBuilder() ; for (int i = 0, j = charAry.Length; i < j; i++)
{ //如果是字母则进行附加;
if (('a' <= charAry[i] && charAry[i] <= 'z') || ('A' <= charAry[i] && charAry[i] <= 'Z'))
{
//这里主要是为了排除最后“填充”的那个字符"a";
if (numOfChar > 0)
{
strBud.Append(currentChar, numOfChar);
} currentChar = charAry[i];
numOfChar = 0;
}//如果是数字则进行数字运算(计算出要附加的字符数),该步骤中运算数字与ansi码有关
else if (0x30 <= charAry[i] && charAry[i] <= 0x39)
{
//如果保存的当前的附加字符的数量为0则简单赋值
if (numOfChar == 0)
{ numOfChar = (int)(charAry[i] ^ 0x30);
//进行进位运算;
}else { numOfChar *= 10;
numOfChar += (int)(charAry[i] ^ 0x30); }
}
}
outString = strBud.ToString();
}
}
}