字符串截取【基础题】

yyjpz2007 2010-05-20 10:23:49
在C#里怎样截取字符串?如从
string strx = "1828490.708;1826267.81;641908.477;639486.574;";中截取这四个数据(不包括;),并放到数组中?
...全文
130 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
string strx = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strx.Split(';');
maguowei19890708 2010-05-20
  • 打赏
  • 举报
回复
二楼正解,这么简单的问题干吗弄的这么麻烦!?
兔子-顾问 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sunle818 的回复:]
引用 11 楼 wuyazhe 的回复:
引用 10 楼 foxdave 的回复:
引用 9 楼 hpzius 的回复:
基本都对,不过有点小问题,应该是:
string strSource = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strSource.Split(new char[]{';'}……
[/Quote]

显然不会异常。如果异常你贴个测试的字符串。

如果考虑楼主的字符串可能出错。或是可能格式中包含非数字的;分割项,用正则最安全;

private static void TestRegex02()
{
double[] numbers = (from m in System.Text.RegularExpressions.Regex.Matches("1828490.708;1826267.81;641908.477;639486.574;",@"\d+(\.\d+)?").Cast<System.Text.RegularExpressions.Match>().ToList() select double.Parse(m.Value)).ToArray();
Array.ForEach(numbers, d => Console.WriteLine(d));
}
zzx509 2010-05-20
  • 打赏
  • 举报
回复

string[] result = strSource.Split(new char[] { ';' },StringSplitOptions.RemoveEmptyEntries); //去空的元素

jianshao810 2010-05-20
  • 打赏
  • 举报
回复
string[] s=strx.Split(';');
skydhx 2010-05-20
  • 打赏
  • 举报
回复
split 方法可以实现
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wuyazhe 的回复:]
引用 10 楼 foxdave 的回复:
引用 9 楼 hpzius 的回复:
基本都对,不过有点小问题,应该是:
string strSource = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strSource.Split(new char[]{';'});

这样会形成一个字符串数组,有5个……
[/Quote]


确实我调试了 的确有5个字符串......因为我尝试用一个长度4的double数组去装,但是出现了异常....
兔子-顾问 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 foxdave 的回复:]
引用 9 楼 hpzius 的回复:
基本都对,不过有点小问题,应该是:
string strSource = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strSource.Split(new char[]{';'});

这样会形成一个字符串数组,有5个结果而不是4个,其中最后一个是空串(因为最后……
[/Quote]

没说错,你那的确是没调试就想当然贴出来的。你写的能通过编译么?没有string.split(char)这样的重载。9楼的严谨是值得肯定的。

我没想说而已。不过我给出的回复都会经过测试。
Justin-Liu 2010-05-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hpzius 的回复:]
基本都对,不过有点小问题,应该是:
string strSource = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strSource.Split(new char[]{';'});

这样会形成一个字符串数组,有5个结果而不是4个,其中最后一个是空串(因为最后还有一个";"),请注意去掉
[/Quote]
你是不懂装懂还是怎么地? 我写的有什么小问题? 什么叫应该是?
hpzius 2010-05-20
  • 打赏
  • 举报
回复
基本都对,不过有点小问题,应该是:
string strSource = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strSource.Split(new char[]{';'});

这样会形成一个字符串数组,有5个结果而不是4个,其中最后一个是空串(因为最后还有一个";"),请注意去掉
兔子-顾问 2010-05-20
  • 打赏
  • 举报
回复
一句话版的……

double[] numbers = (from s in "1828490.708;1826267.81;641908.477;639486.574;".Split(";".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList() select double.Parse(s)).ToArray();
Array.ForEach(numbers, d => Console.WriteLine(d));
lyy_0814 2010-05-20
  • 打赏
  • 举报
回复
楼上的方法是对的,用Split就可以了
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 comaple 的回复:]
string strx = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strx.Split(';');
[/Quote]
过滤分号?
康派尔 2010-05-20
  • 打赏
  • 举报
回复
string strx = "1828490.708;1826267.81;641908.477;639486.574;";
string [] result=strx.Split(';');
睡神在睡觉 2010-05-20
  • 打赏
  • 举报
回复
string strx = "1828490.708;1826267.81;641908.477;639486.574;";
string[] str=strx.Split(";");
koukoujiayi 2010-05-20
  • 打赏
  • 举报
回复
string[] s=strx.Split(';');
应该是单引号
koukoujiayi 2010-05-20
  • 打赏
  • 举报
回复
string[] s=strx.Split(";");
Justin-Liu 2010-05-20
  • 打赏
  • 举报
回复
string[] result = strx.Split(';');

111,097

社区成员

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

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

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