[英]How to split a string with a certain character without considering the length of the character?
I'm trying to work on this string 我正在尝试处理此字符串
abc
def
--------------
efg
hij
------
xyz
pqr
--------------
Now I have to split the string with the -
character. 现在,我必须用
-
字符分割字符串。 So far I'm first spliting the string in lines and then finding the occurrence of -
and the replacing the line with a single *, then combining the whole string and splitting them again. 到目前为止,我第一次在劈裂行字符串,然后找到的发生
-
与一个单一的*替换行,那么整个字符串,分裂他们再次结合。
I'm trying to get the data as 我正在尝试获取数据
string[] set =
{
"abc
def",
"efg
hij",
"xyz
pqr"
}
Is there a better way to do this? 有一个更好的方法吗?
var spitStrings = yourString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
如果我理解您的问题,以上代码即可解决。
I'm confused with exactly what you're asking, but won't this work? 我对您的要求感到困惑,但这行不通?
string[] seta =
{
"abc\ndef",
"efg\nhij",
"xyz\npqr"
}
\\n = CR (Carriage Return) // Used as a new line character in Unix
\\r = LF (Line Feed) // Used as a new line character in Mac OS
\\n\\r = CR + LF // Used as a new line character in Windows
(char)13 = \\n = CR // Same as \\n
If I'm understanding your question about splitting -
's then the following should work. 如果我了解您的有关拆分
-
的问题,则以下内容应适用。
string s = "abc-def-efg-hij-xyz-pqr"; // example?
string[] letters = s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
If this is what your array looks like at the moment, then you can loop through it as follows: 如果这是您的阵列目前的样子,则可以如下遍历它:
string[] seta = {
"abc-def",
"efg-hij",
"xyz-pqr"
};
foreach (var letter in seta)
{
string[] letters = letter.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
// do something with letters?
}
Use of string split function using the specific char or string of chars (here -) can be used. 可以使用使用特定字符的字符串拆分功能,也可以使用字符字符串(此处为-)。 The output will be array of strings.
输出将是字符串数组。 Then choose whichever strings you want.
然后选择所需的任何字符串。 Example:
例:
I'm sure this below code will help you... 我确定下面的代码可以帮助您...
string m = "adasd------asdasd---asdasdsad-------asdasd------adsadasd---asdasd---asdadadad-asdadsa-asdada-s---adadasd-adsd";
var array = m.Split('-');
List<string> myCollection = new List<string>();
if (array.Length > 0)
{
foreach (string item in array)
{
if (item != "")
{
myCollection.Add(item);
}
}
}
string[] str = myCollection.ToArray();
if it does then don't forget to mark my answer thanks....;) 如果这样的话,别忘了标记我的答案谢谢....;)
string set = "abc----def----------------efg----hij--xyz-------pqr" ;
var spitStrings = set.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
EDIT - 编辑-
He wants to split the strings no matter how many '-' are there. 无论有多少个“-”,他都想分割字符串。
var spitStrings = set.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
This will do the work. 这样就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.