如何在字符串数组中为分号后指定拆分字符串? [英] how to specify split string for after semicolon in string array?

查看:88
本文介绍了如何在字符串数组中为分号后指定拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string [] Query = query.Split(';');我正在使用这个用于拆分string.i想要在semicolon.i之前的分割字符的semicolon.given代码之后如何拆分以及分号之后的代码。

解决方案

< blockquote>如果你想分开单词;在这之后接受这个词;

这样做



例如:字符串查询=xxx; yyy

string [] Query = query.Split(';');





查询[0]将是xxx

查询[1]将是yyy


你不能使用string.Split在一个字符后拆分 - 它在一个字符上拆分一个字符串:所以它删除了拆分指示符,并返回两个(或更多)不包含拆分的字符串性格。正则表达式也做同样的事情,但可以应对更复杂的匹配模式,所以他们赢了; t帮助你。



你可以做三件事:

1)编写自己的拆分方法,不删除拆分字符。

2)使用拆分,然后将拆分字符添加回每行后面。

3)使用String.Replace在每个分号后添加一个特殊字符,然后拆分。



第二个选项可能由于创建的字符串数量加倍,效率最低。


string[] Query = query.Split(';'); i am using this for for split string.i want code for how i split after semicolon.given code for split character in before semicolon.i want code for after semicolon.

解决方案

If you want to split the word from ; and take the word after the ;
do like this

Eg: string query = "xxx;yyy"
string[] Query = query.Split(';');


Query [0] will be xxx
Query [1] will be yyy


You can't use string.Split to split after a character - it splits a string on a character: so it remove the split indicator, and returns two (or more) strings which do not contain the split character at all. Regular expressions also do the same thing, but can cope with a more complex matching pattern, so they won;t help you either.

There are three things you can do:
1) Write your own split method with does not remove the split character.
2) Use Split, and then add the split character back into each line afterwards.
3) Use String.Replace to add a "special character" after each semicolon, then split on that.

The second option would probably be the least efficient as the number of strings created is doubled.


这篇关于如何在字符串数组中为分号后指定拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆