Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split with delimiter in C#/ASP.Net

If I do this:

 string text = "Hello, how are you?";

 string[] split = text.Split('h', 'o');

How do I get a list of what delimiter was used between each split? I'm trying to recreate the string as a whole.

like image 904
cdub Avatar asked Aug 26 '11 19:08

cdub


People also ask

What is delimiter in C?

A delimiter is one or more characters that separate text strings. Common delimiters are commas (,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ ). When a program stores sequential or tabular data, it delimits each item of data with a predefined character.

What is strtok function in C?

Apr 07, 2007. The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.

How split a string after a specific character in C #?

Split(char[], StringSplitOptions) Method This method is used to splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements. Syntax: public String[] Split(char[] separator, StringSplitOptions option);

Can a string be a delimiter?

In Java, delimiters are the characters that split (separate) the string into tokens. Java allows us to define any characters as a delimiter. There are many string split methods provides by Java that uses whitespace character as a delimiter. The whitespace delimiter is the default delimiter in Java.


3 Answers

As @Davy8 mentioned, there is no built in way. Here's a VERY simple example to get you going on writing a custom method.

void Main()
{
    string text = "Hello, how are you?";
    List<SplitDefinition> splitDefinitionList = CustomSplit(text, new char[] { 'h', 'o' });
}

public List<SplitDefinition> CustomSplit(string source, char[] delimiters)
{
    List<SplitDefinition> splitDefinitionList = new List<SplitDefinition>();

    foreach(char d in delimiters)
    {
        SplitDefinition sd = new SplitDefinition(d, source.Split(d));           
        splitDefinitionList.Add(sd);
    }

    return splitDefinitionList;
}

public class SplitDefinition
{
    public SplitDefinition(char delimiter, string[] splits)
    {
        this.delimiter = delimiter;
        this.splits = splits;
    }

    public char delimiter { get; set; }
    public string[] splits { get; set; }
}
like image 74
James Hill Avatar answered Oct 17 '22 02:10

James Hill


There isn't a built in way that I'm aware of. You're probably better off writing your own custom split method that keeps track of the delimiters.

like image 2
Davy8 Avatar answered Oct 17 '22 04:10

Davy8


This is impossible. The string has been split, so how can you possibly know if the split was based on a 'h' or an 'o'?

Anyways if you can do this:

 string[] split = text.Split('h', 'o');

then why not also store those characters?

like image 1
m.edmondson Avatar answered Oct 17 '22 03:10

m.edmondson