给字符串分组 string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";每三个字符分成一个新数组 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 循环一下长度就可以咯感觉楼主是按逗号分吧?可以用string.Split(',') split如何把字符串从第三个处分开??? 先用string.split(','); 将一个个的字符串放到数组中 然后再用for循环 每三个一组 记得要加上 "," string.replace(",",""); //替换掉逗号再用for循环 可以减小空间复杂度(去掉了一个临时的数组)^_^ string str = "df,122,12e,12g,12e,12d2,12d2,12d2,"; str.Replace(",", "");str字符串的值为:"df12212e12g12e12d212d212d2"通过for循环将str字符串3个字符为一组进行分组。 string str = ""; int i = 0; Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>(); List<String> lst = new List<string>(); string[] sp = str.Split(new char[1] { ',' }); foreach (string s in sp) { if (i % 3 == 0) { dic.Add(i, lst); lst.Clear(); } else lst.Add(s); i += 1; } foreach (KeyValuePair<int, List<string>> kvp in dic) { } static string[] Split(string str) { char[] crs = str.ToCharArray(); List<string> ret = new List<string>(); int i = 0; string tmp=string.Empty; foreach (char ch in crs) { if (ch == ',') continue; tmp += ch; if (i == 2) { ret.Add(tmp); tmp = string.Empty; i = 0; } else { i++; } } if (tmp.Length > 0) { ret.Add(tmp); } return ret.ToArray(); }我应该好好练习下算法了。哎 string newstr=null; string str = "df,122,12e,12g,12e,12d2,12d2,12d2,"; str = str.Replace(",",""); for (int i = 1; i < str.Length; i++) { newstr = newstr + str.Substring(i, 1); if (i % 3 == 0 && i !=0) { newstr = newstr + ","; } } Response.Write(newstr); 非常感谢.viewstate.感觉还行 怎么引用不了呀,强类型是如何引用的.我的报错 CS0246: 找不到类型或命名空间名称“List”(是否缺少 using 指令或程序集引用?) List<string> _L = new List<string>(); while (str.IndexOf(",") > 0) { int i = str.IndexOf(",", str.IndexOf(",", str.IndexOf(",") + 1) + 1); string t = ""; if (i < 0) { t = str; str = str.Remove(0, str.Length); } else { t = str.Substring(0, i); str = str.Remove(0, i + 1); } _L.Add(t); } string[] sArry ={ _L[0], _L[1], _L[2] }; for (int i = 0; i < sArry.Length; i++) { Console.WriteLine(sArry[i]); Console.Write(""); } ”找不到类型或命名空间名称“List”(是否缺少 using 指令或程序集引用?)“用的楼上的list的DEMO吧 说的很明显了 缺少 using 指令或程序集引用 我加了这一句呀using System.Collections是不是还要加别的 /// <summary> /// /// </summary> /// <param name="str">要分割的字符串</param> /// <param name="f">分割的组数</param> /// <returns></returns> static string[] _Result(string str, int f) { List<string> _L = new List<string>(); string[] sArry = str.Split(','); for (int i = 0; i < sArry.Length / f; i++) { string _s = ""; for (int j = 0; j < sArry.Length; j++) { if (i == j / f) { _s += sArry[j].ToString() + ','; } } _L.Add(_s); } return _L.ToArray(); } string str = "df,122,12e,12g,12e,12d2,12d2,12d2,"; str=str.Replace(",",""); int count=0; List<string> lists = new List<string>(); while (count <= str.Length-1) { if (count + 3 > str.Length) { lists.Add(str.Substring(count)); } else { lists.Add(str.Substring(count, 3)); } count += 3; } foreach (string list in lists) { Response.Write(list+"<br/>"); } 楼主用的.NET哪个版本?try...string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";Regex reg = new Regex(@"([^,]*,){3}|([^,]*,)+$");MatchCollection mc = reg.Matches(str);foreach(Match m in mc){ richTextBox2.Text += m.Value + "\n";} 客户端脚本注册中 那个type 是什么意思? 请问-- aspx.cs文件中的DataSet,如何传递到js的recordSet中? 商品表应该包括哪些字段 怎么在我的网站上添加音乐,然后可以在线听,也可以下载的功能! ASP.NET做论坛 关于asp.net的下拉菜单 $$$$$$$$这个问题可能只有李开复才能解决了¥¥¥¥¥¥¥¥¥¥¥¥¥¥ updatepanel不是已知元素,怎样解决? 离奇事件-aspx调用word文件 easyui tree +asp.net 动态添加 =号附近有语法错误!怎么回事儿,进来指教下! fckeditor上传问题
然后再用for循环 每三个一组 记得要加上 ","
再用for循环 可以减小空间复杂度(去掉了一个临时的数组)^_^
string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
str.Replace(",", "");str字符串的值为:"df12212e12g12e12d212d212d2"
通过for循环将str字符串3个字符为一组进行分组。
int i = 0;
Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
List<String> lst = new List<string>();
string[] sp = str.Split(new char[1] { ',' });
foreach (string s in sp)
{
if (i % 3 == 0)
{
dic.Add(i, lst);
lst.Clear();
}
else
lst.Add(s);
i += 1;
}
foreach (KeyValuePair<int, List<string>> kvp in dic)
{ }
static string[] Split(string str)
{
char[] crs = str.ToCharArray();
List<string> ret = new List<string>();
int i = 0;
string tmp=string.Empty;
foreach (char ch in crs)
{
if (ch == ',')
continue;
tmp += ch;
if (i == 2)
{
ret.Add(tmp);
tmp = string.Empty;
i = 0;
}
else
{
i++;
}
}
if (tmp.Length > 0)
{
ret.Add(tmp);
}
return ret.ToArray();
}我应该好好练习下算法了。哎
string newstr=null;
string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
str = str.Replace(",","");
for (int i = 1; i < str.Length; i++)
{
newstr = newstr + str.Substring(i, 1);
if (i % 3 == 0 && i !=0)
{
newstr = newstr + ",";
}
}
Response.Write(newstr);
非常感谢.viewstate.
感觉还行
怎么引用不了呀,强类型是如何引用的.
我的报错 CS0246: 找不到类型或命名空间名称“List”(是否缺少 using 指令或程序集引用?)
List<string> _L = new List<string>();
while (str.IndexOf(",") > 0)
{
int i = str.IndexOf(",", str.IndexOf(",", str.IndexOf(",") + 1) + 1);
string t = "";
if (i < 0)
{
t = str;
str = str.Remove(0, str.Length);
}
else
{
t = str.Substring(0, i);
str = str.Remove(0, i + 1);
}
_L.Add(t);
}
string[] sArry ={ _L[0], _L[1], _L[2] }; for (int i = 0; i < sArry.Length; i++)
{
Console.WriteLine(sArry[i]);
Console.Write("");
}
我加了这一句呀
using System.Collections是不是还要加别的
///
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <param name="f">分割的组数</param>
/// <returns></returns>
static string[] _Result(string str, int f)
{
List<string> _L = new List<string>();
string[] sArry = str.Split(','); for (int i = 0; i < sArry.Length / f; i++)
{
string _s = "";
for (int j = 0; j < sArry.Length; j++)
{
if (i == j / f)
{
_s += sArry[j].ToString() + ',';
}
}
_L.Add(_s);
}
return _L.ToArray();
}
string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
str=str.Replace(",","");
int count=0;
List<string> lists = new List<string>();
while (count <= str.Length-1)
{
if (count + 3 > str.Length)
{
lists.Add(str.Substring(count));
}
else
{
lists.Add(str.Substring(count, 3));
}
count += 3;
}
foreach (string list in lists)
{
Response.Write(list+"<br/>");
}
楼主用的.NET哪个版本?try...string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
Regex reg = new Regex(@"([^,]*,){3}|([^,]*,)+$");
MatchCollection mc = reg.Matches(str);
foreach(Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}