问题描述
如果我有一个字符串: STR1 | STR2 | STR3 | SRT4
和解析|
作为分隔符。我的输出是 STR1 STR2赛车STR3 STR4
但是,如果我有一个字符串: STR1 || STR3 | STR4
输出将 STR1 STR3 STR4
。我在寻找什么我的输出要像为 STR1空/空白STR3 STR4
。
我希望这说得通。
字符串createText =SRT1 || STR3 | STR4
的String [] = TXT createText.Split(新[] {'|',','}
StringSplitOptions.RemoveEmptyEntries);
如果(File.Exists(路径))
{
//Console.WriteLine(\"{0}已经存在,路径)。
File.Delete(路径);
//写入文件。使用
(StreamWriter的SW =新的StreamWriter(路径,真实,Encoding.Unicode))
{
sw.WriteLine(STR1:{0},TXT [0] );
sw.WriteLine(STR2:{0},TXT [1]);
sw.WriteLine(STR3:{0},txt的[2]);
sw.WriteLine(STR4:{0},TXT [3]);
}
}
输出
$ B STR1
STR2:STR3
STR3:STR4
STR4:空白
STR1 >
那不是我要找的。这是我想编写的内容:
STR1:STR1
STR2:空白
STR3 :STR3
STR4:STR4
最简单的的方式是使用:在
使用System.Text.RegularExpressions;
...
的String [] =部分新正则表达式([|] +)分裂(STR1 | STR2 | STR3 | SRT4);
的
+摆脱它。
维基百科:
+加号表示,有一个以上前面的元素。例如,AB + C匹配ABC,ABBC,abbbc,等等,但不是交流
表格方法类似于String.Split方法,除了拆分拆分串,对由正则表达式,而不是一组字符的确定的分隔符。输入字符串被分割为多次越好。如果在输入字符串中没有找到图案,返回值包含一个元素,其值是原始输入字符串
附加的愿望,可以用做:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名ConsoleApplication1 {
类节目{
静态无效的主要(字串[] args){
的String []部分=STR1 || STR2 | STR3。更换(@||,| \blank\|)斯普利特(@|)。
的foreach(字符串s的部分)
Console.WriteLine(S);
}
}
}
If I have a string: str1|str2|str3|srt4
and parse it with |
as a delimiter. My output would be str1 str2 str3 str4
.
But if I have a string: str1||str3|str4
output would be str1 str3 str4
. What I'm looking for my output to be like is str1 null/blank str3 str4
.
I hope this makes sense.
string createText = "srt1||str3|str4";
string[] txt = createText.Split(new[] { '|', ',' },
StringSplitOptions.RemoveEmptyEntries);
if (File.Exists(path))
{
//Console.WriteLine("{0} already exists.", path);
File.Delete(path);
// write to file.
using (StreamWriter sw = new StreamWriter(path, true, Encoding.Unicode))
{
sw.WriteLine("str1:{0}",txt[0]);
sw.WriteLine("str2:{0}",txt[1]);
sw.WriteLine("str3:{0}",txt[2]);
sw.WriteLine("str4:{0}",txt[3]);
}
}
Output
str1:str1
str2:str3
str3:str4
str4:"blank"
Thats not what i'm looking for. This is what I would like to code:
str1:str1
str2:"blank"
str3:str3
str4:str4
The simplest way is to use Quantification:
using System.Text.RegularExpressions;
...
String [] parts = new Regex("[|]+").split("str1|str2|str3|srt4");
The "+" gets rid of it.
From Wikipedia :"+" The plus sign indicates that there is one or more of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
Form msdn: The Regex.Split methods are similar to the String.Split method, except Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The input string is split as many times as possible. If pattern is not found in the input string, the return value contains one element whose value is the original input string.
Additional wish can be done with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class Program{
static void Main(string[] args){
String[] parts = "str1||str2|str3".Replace(@"||", "|\"blank\"|").Split(@"|");
foreach (string s in parts)
Console.WriteLine(s);
}
}
}
这篇关于C#字符串分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!