怎么获得字符串中所有逗号的位置

巩师磊 2009-02-04 07:03:09
怎么获得字符串中所有逗号的位置
如"125,4852,892,982,9272,581,2585,25898962,586122,98254,";
...全文
378 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cacaca6 2009-02-10
  • 打赏
  • 举报
回复
我Call,各位老大,用Linq呀,超级简单,就一条语句

string str = "125,4852,892,982,9272,581,2585,25898962,586122,98254,";
foreach (var n in str.ToCharArray().Select((s, n) => new KeyValuePair<int, char>(n, s))
.Where(s => s.Value == ',')
.Select(s => s.Key)
)
{
Console.WriteLine(n);
}
sxmonsy 2009-02-06
  • 打赏
  • 举报
回复

string str = "fkfjaf,dfasfasfas,asfasdf,,,,";
int number = 0;
foreach (char chr in str)
{
if (chr == ',')
{
number ++;
}
}

liujiayu10 2009-02-06
  • 打赏
  • 举报
回复
IndexOf 只要用这个方法就可以了嘛,干嘛搞得那么复杂呢
真相重于对错 2009-02-06
  • 打赏
  • 举报
回复
String.IndexOf (
char value,
int startIndex
)
zpcoder 2009-02-06
  • 打赏
  • 举报
回复
try

string str1 = "125,4852,892,982,9272,581,2585,25898962,586122,98254,"; 
string a;
int i=0;
while(str.Contains(",")) //不好意思,单词忘了,这里好像是个Contains(",")
{
i=str1.IndexOf(",");
a = str1.Substring(0,i);
this.TextBox1.Text += i.ToString() + ";";
}
hongmaohouzi 2009-02-05
  • 打赏
  • 举报
回复
string str1 = "125,4852,892,982,9272,581,2585,25898962,586122,98254,";
int strlong = str1.Length;
for (int i = 0; i < Convert.ToInt32(str1.Length); i++)
{
string a = str1.Substring(i,1).ToString();
if (str1.Substring(i,1).ToString() == ",")
{
int j = 0;
j = i + 1;
this.TextBox1.Text = this.TextBox1.Text + j + ";";
}
else
{
this.TextBox1.Text = this.TextBox1.Text + "#";
}
}

你看看是否满足你的要求!
perhaplove 2009-02-04
  • 打赏
  • 举报
回复
正则
机器人 2009-02-04
  • 打赏
  • 举报
回复
 Dim str As String = "125,4852,892,982,9272,581,2585,25898962,586122,98254,"
Dim lstIndex As List(Of Integer) = New List(Of Integer)
For i As Integer = 0 To str.Length - 1
If str(i) = ","c Then
lstIndex.Add(i)
End If
Next
wuyq11 2009-02-04
  • 打赏
  • 举报
回复
分割字符串.通过循环
把字符串和长度保存到数组或泛型.
string s;
int len=0;
string[] str=s.split(',');
for(int i=0;i<str.length;i++)
{
//长度值循环添加
}

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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