字符串截取问题

maycxd 2011-10-24 07:52:40
请问比如说A,B,"C,D,E","F,G,
我想截取A B "C,D,E" "F G

有什么好的办法可以截取吗?写1大堆代码,感觉都是垃圾代码,重复的太多了,求高手
有什么简单的截取办法~!
...全文
153 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixiaobo520 2011-10-25
  • 打赏
  • 举报
回复
如何才能有分啊?
hudenq 2011-10-25
  • 打赏
  • 举报
回复
   string strt = "";
string str ;
str=str.Replace(",\""," ");
string[] str1 = str.Split(' ');
for (int i = 0; i < str1.Length; i++)
{
int dp = str1[i].IndexOf('"');
if (dp == -1)
{
strt += str1[i].Replace(",", "")+" ";

}
else
{
strt += str1[i]+" ";

}
strt=strt.trim().replace(" ",",\"");
}
q198708wyp 2011-10-25
  • 打赏
  • 举报
回复
你 要处理这样的 感觉 可以在存储过程中实现 省的 winform界面看起来 很乱的感觉
krenyelang 2011-10-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuchonglei 的回复:]
split
[/Quote]
它是将所有的,都要去掉,所以不可!
尝试用替换操作实现看……
lh412552703 2011-10-25
  • 打赏
  • 举报
回复
谢谢谢谢谢谢谢谢
maycxd 2011-10-25
  • 打赏
  • 举报
回复
谢谢了大家了,还是给我了1点思路,我稍微重写了1下我自己的代码,稍微简明点了~!
结贴OVER~!
fengyunyin 2011-10-25
  • 打赏
  • 举报
回复
想了一个办法,用队列,根据','和'\"'来判断将什么添加到队列。

string s = "A,B,\"C,D,E\",\"F,G,";
Queue<string> que = new Queue<string>();
string temp = "";
bool bjoin = false;
foreach(char c in s.ToCharArray())
{
if (bjoin == false)
{
if (c == ',')
continue;
if (c == '\"')
{
bjoin = true;
temp += c.ToString();
continue;
}
que.Enqueue(c.ToString());
}
else
{
if (c == '\"')
{
bjoin = false;
temp += c.ToString();
que.Enqueue(temp);
temp = "";
}
else
{
temp += c.ToString();
}
}
}
if (temp != "")
{
string[] strs = temp.Split(',');
foreach (string str in strs)
{
que.Enqueue(str);
}
}
string[] array = que.ToArray();
maycxd 2011-10-24
  • 打赏
  • 举报
回复
,好吧,只要保留2个双引号中的内容,不要考虑别的规则,现在只希望谁给我点好的思路
xiongxyt2 2011-10-24
  • 打赏
  • 举报
回复
没看懂你的规律,如果是不要逗号的话,可以用split

程序代码
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):

1. public string[] Split(params char[] separator)

程序代码
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}

2. public string[] Split(char[] separator, int count)

程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}

3. public string[] Split(char[] separator, StringSplitOptions options)

程序代码
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

4. public string[] Split(string[] separator, StringSplitOptions options)

程序代码
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

5. public string[] Split(char[] separator, int count, StringSplitOptions options)

程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

6. public string[] Split(string[] separator, int count, StringSplitOptions options)

程序代码
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素


需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')!很多人都很奇怪为什么把双引号改为单引号就可以了?看了上边的重载函数该知道答案了吧^_^

maycxd 2011-10-24
  • 打赏
  • 举报
回复
各抒己见啊,只要最后的结果,方法都是想出来的,可以呀
10楼你这。。。写死了吧。。我的只是例子
轻狂小疯 2011-10-24
  • 打赏
  • 举报
回复
可不可以在字符串里面有规律的添加一些符号呢 。。根据符号截取。。!!
黄亮 2011-10-24
  • 打赏
  • 举报
回复

string s = "A,B,\"C,D,E\",'F,G,";
var builder = new StringBuilder(s.Length);
var strings = s.Split('\"');
for (int index = 0; index < strings.Length; index += 2)
{

strings[index] = strings[index].Replace(","," ");

}
MessageBox.Show(string.Join("\"", strings));
maycxd 2011-10-24
  • 打赏
  • 举报
回复
在补充下,基本都是2个“”1对,中间内容保留,然后单个“保留到下个,前的内容,6个例子CSV都是这样
应该没别的情况了
maycxd 2011-10-24
  • 打赏
  • 举报
回复
恩,不好意思回家晚了,下雨天好难受 >_<
A,B,"C,D,E","F,G,

基本上双引号内保持不变,单引号前后的字符串前面保留到,号 ,然后F"
这种情况应该没有,貌似这样,这是客户给的CSV呀 = =,做个东西,首先截取
字符串,写的我好郁闷,一直看SQL,C#又忘的差不多了
  • 打赏
  • 举报
回复
写个方法,遇到,替换,遇到"继续找下一个",找到后遇到,再替换
  • 打赏
  • 举报
回复
有的话,真的是无规律了,
  • 打赏
  • 举报
回复
还有会不会有这种情况呢:A,B"C,D,E","F,G,
这种呢:A,B"""C,D,E","F,G,
规则都没说清楚,叫人情何以堪啊
  • 打赏
  • 举报
回复
会不会有A,B","C,D,E","F,G,
这种情况呢
黄亮 2011-10-24
  • 打赏
  • 举报
回复
看不懂你的规律,本来以为是双引号内不处理,仔细看又不是。
maycxd 2011-10-24
  • 打赏
  • 举报
回复
下班回家了,到家了在看看又什么好的办法没,不大懂正则,解决就结贴~!
加载更多回复(1)

110,537

社区成员

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

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

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