views:

136

answers:

5

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
A: 

Try regex split. http://msdn.microsoft.com/en-us/library/ze12yx1d.aspx
"If multiple matches are adjacent to one another, an empty string is inserted into the array."

crtracy
How exactly is the regex split supposed to work in the scenario the OP is asking?
Esteban Araya
System.String.Split() will eat the empty value (return array with length 3), but Regex.Split will keep put an empty string in that position (array with length 4). I thought the OP wanted an empty string in the returned array of strings as the 2nd element, but I now realize he wanted the actual string "null/blank", my bad.
crtracy
+6  A: 

Try this one:

str.Split('|')

Without StringSplitOptions.RemoveEmptyEntries passed, it'll work as you want.

Mehrdad Afshari
I misread the question, what I thought OP want is what he doesn't want..haha.sorry I'll change vote-down to vote-up in 8 minutes because SO locks my vote function now :(
Danny Chen
SO tells me "You last voted on this answer 14 mins ago Your vote is now locked in unless this answer is edited". So I can't vote to your answer until you edit it? Sorry for my mistake.
Danny Chen
OK I change -1 to +1 now
Danny Chen
+5  A: 

this should do the trick...

string s = "str1||str3|str4";
string[] parts = s.Split('|');
Lee
+2  A: 

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);
        }
    }
}
Margus
Wow...this works for what i'm trying to do with streamWriter
Chad Sellers
Thanks a million.....
Chad Sellers
This may be a "cool" way to use `Regex`, but it's a very round-about solution to the question that was posed. The OP simply wanted to preserve empty segments, and the `Split` function does this by default -- unless you pass the option `StringSplitOptions.RemoveEmptyEntries`. So the OP could have solved the original problem by simply removing the unneeded option from the call to `Split` in the original example code. Building and compiling a regular expression is way more overhead (and way more complication) than is needed for this case.
Lee
A: 

Try something like this:

string result = "str1||str3|srt4";
List<string> parsedResult = result.Split('|').Select(x => string.IsNullOrEmpty(x) ? "null" : x).ToList();

when using the Split() the resulting string in the array will be empty (not null). In this example i have tested for it and replaced it with the actual word null so you can see how to substitute in another value.

slugster