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.
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.
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.
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);
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.
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; }
}
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With