拆分字符串数组特定位置C# [英] Split string to specific array position c#

查看:149
本文介绍了拆分字符串数组特定位置C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊的情况,我需要创建在零位空字符串数组,然后分割字符串的其余位置。例如,我想要输入以下字符串:

I have a unique situation where I need to create string arrays that have null in position zero and then a split string for the remaining positions. For example, I want to input the following string:

"this/\is/\a/\test/\string"

输出结果如下

array[0] = null;
array[1] = "this";
array[2] = "is";
array[3] = "a";
array[4] = "test";
array[5] = "string";

我知道我可以与一些code线的笨方法做到这一点,但我正在寻找一个优雅的方式来将字符串分割到一个特定的阵列的位置,或者只是在它前面插入空。对于位置0一个空字符串也是可以接受的,但效果并不理想。有没有做到这一点的好办法?阵列位置的数量会有所不同,因为输入字符串都会有不同的我需要拉参数的数字。

I know I can accomplish this in a clunky way with a number of lines of code, but I'm looking for an elegant way to split a string to a specific array position, or just to insert null before it. An empty string for position 0 would also be acceptable but not ideal. Is there a good way to accomplish this? The number of array positions will vary, as the input string will have varying numbers of parameters I need to pull.

推荐答案

prePEND您的分隔符的字符串,然后设置分割后空的第一个元素:

Prepend your delimiter to the string and then set the first element to null after the split:

string data = @"this/\is/\a/\test/\string";
string[] result = (@"/\" + data).Split(new[] {@"/\"}, StringSplitOptions.None);
result[0] = null;

这篇关于拆分字符串数组特定位置C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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