How do I split a string with a string in C# .net 1.1.4322?
String example:
Key|Value|||Key|Value|||Key|Value|||Key|Value
need:
Key|Value
Key|Value
Key|Value
I cannot use the RegEx.Split because the separating character is the ||| and just get every character separately.
I cannot use the String.Split() overload as its not in .net 1.1
Example of Accepted solution:
using System.Text.RegularExpressions;
String[] values = Regex.Split(stringToSplit,"\\|\\|\\|");
What about using #"\|\|\|" in your Regex.Split call? That makes the | characters literal characters.
One workaround is replace and split:
string[] keyvalues = "key|value|||key|value".replace("|||", "~").split('~');
here is an example:
System.Collections.Hashtable table;
string[] items = somestring.split("|||");
foreach(string item in items)
{
string[] keyvalue = item.split("|");
table.add(keyvalue[0],keyvalue[1]);
}
string input = "Hi#*#Hello#*#i#*#Hate#*#My#*#......" ;
string[] delim = new string[] { "#*#" };
string[] results = input.split(delim , StringSplitOptions.None);
Related
This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 4 years ago.
I have this string:
"My name is Marco and I'm from Italy"
I'd like to split it, with the delimiter being is Marco and, so I should get an array with
My name at [0] and
I'm from Italy at [1].
How can I do it with C#?
I tried with:
.Split("is Marco and")
But it wants only a single char.
string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);
If you have a single character delimiter (like for instance ,), you can reduce that to (note the single quotes):
string[] tokens = str.Split(',');
.Split(new string[] { "is Marco and" }, StringSplitOptions.None)
Consider the spaces surronding "is Marco and". Do you want to include the spaces in your result, or do you want them removed? It's quite possible that you want to use " is Marco and " as separator...
You are splitting a string on a fairly complex sub string. I'd use regular expressions instead of String.Split. The later is more for tokenizing you text.
For example:
var rx = new System.Text.RegularExpressions.Regex("is Marco and");
var array = rx.Split("My name is Marco and I'm from Italy");
Try this function instead.
string source = "My name is Marco and I'm from Italy";
string[] stringSeparators = new string[] {"is Marco and"};
var result = source.Split(stringSeparators, StringSplitOptions.None);
You could use the IndexOf method to get a location of the string, and split it using that position, and the length of the search string.
You can also use regular expression. A simple google search turned out with this
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
string value = "cat\r\ndog\r\nanimal\r\nperson";
// Split the string on line breaks.
// ... The return value from Split is a string[] array.
string[] lines = Regex.Split(value, "\r\n");
foreach (string line in lines) {
Console.WriteLine(line);
}
}
}
Read C# Split String Examples - Dot Net Pearls and the solution can be something like:
var results = yourString.Split(new string[] { "is Marco and" }, StringSplitOptions.None);
There is a version of string.Split that takes an array of strings and a StringSplitOptions parameter:
http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
How can I split a word from within brackets like:
(animal)
I need to take only the word "animal" using C# split.
If you only want to split on brackets this will do:
string test = "(duck)(monkey)";
string[] animals = test.Split(new [] {'(', ')'},
StringSplitOptions.RemoveEmptyEntries);
animals now contains { "duck", "monkey"}. For a single animal input (i.e. (animal)) just take animals[0] or evaluate directly:
string animal = test.Split(new [] {'(', ')'},
StringSplitOptions.RemoveEmptyEntries)[0];
The documentation for the String.Split method already gives you examples of how to do this. Just specify the brackets as the delimiter characters you want to split on:
string originalString = "(animal)";
string[] newString = originalString.Split(new char[] {'(', ')'});
Output:
{"", "animal", ""}
Are you sure you need to use split()?
If it is as simple as you stated wouldn't
string justWord = "(animal)".Replace("(","").Replace(")","")
be more efficient and clearer?
Only the trim is enough to do this
string originalString = "(animal)";
originalString = originalString.Trim('(',')');
Here is
string searchValues = "(duck)(monkey)";
var matches = Regex.Matches(searchValues, #"\w+");
var values = (from matche in matches.Cast<Match>() select matche.Value).ToList();
i want to split the String = "Asaf_ER_Army" by the "ER" seperator.
the Split function of String doesn't allow to split the string by more than one char.
how can i split a string by a 'more than one char' seperator?
It does. Read here.
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
// Split a string delimited by another string and return all elements.
string[] result = source.Split(stringSeparators, StringSplitOptions.None);
Edit:
Alternately, you can have some more complicated choices (RegEx). Here, http://dotnetperls.com/string-split.
String.Split does do what you want. Use the overload that takes a string array.
Example:
string[] result = "Asaf_ER_Army".Split(
new string[] {"ER"},
StringSplitOptions.None);
Result:
Asaf_
_Army
There is an overload of String.Split which takes a string array as separators: http://msdn.microsoft.com/en-gb/library/1bwe3zdy%28v=VS.80%29.aspx
Unless you are using framework < 2?
I have textbox in c#, contains two or three strings with white spaces. i want to store those strings seperately.please, suggest me any code. thanx.
var complexValue = #"asdfasdfsdf asdfasd fffff
asdfasdfasdf";
var complexValues = complexValue.Split();
NOTICE:
.Split() is a pseudo-overload, as it gets compiled as .Split(new char[0]).
additionally msdn tells us:
If the separator parameter is
null or contains
no characters, white-space characters
are assumed to be the delimiters.
White-space characters are defined by
the Unicode standard and return true
if they are passed to the
Char.IsWhiteSpace method.
Firstly use this name space
using System.Text.RegularExpressions;
in your code
string Message = "hi i am fine";
string []Record=Regex.Split(Message.Trim(), " ");
Output is an array.
I hope it works.
string[] parts = myTextbox.Text.Split();
Calling String.Split() with no parameters will force the method to consume all whitespace and only return the separated strings:
var individualStrings = originalString.Split();
To get three different strings in an array, you can use String.Split()
string[] myStringArray = OriginalString.Split(" ".ToCharArray());
string[] words = Regex.Split(textBox.Text, #"\s+");
Try this:
string str = #"this is my string";
string[] arr = str.Split(new char[] { char.Parse(" ") });
Try :
string data = TextBox1.Text;
var s1 = data.Split();
string a = s1[0].ToString();
string b= s1[1].ToString();
i am having trouble splitting a string in c# with a delimiter of "][".
For example the string "abc][rfd][5][,][."
Should yield an array containing;
abc
rfd
5
,
.
But I cannot seem to get it to work, even if I try RegEx I cannot get a split on the delimiter.
EDIT: Essentially I wanted to resolve this issue without the need for a Regular Expression. The solution that I accept is;
string Delimiter = "][";
var Result[] = StringToSplit.Split(new[] { Delimiter }, StringSplitOptions.None);
I am glad to be able to resolve this split question.
To show both string.Split and Regex usage:
string input = "abc][rfd][5][,][.";
string[] parts1 = input.Split(new string[] { "][" }, StringSplitOptions.None);
string[] parts2 = Regex.Split(input, #"\]\[");
string tests = "abc][rfd][5][,][.";
string[] reslts = tests.Split(new char[] { ']', '[' }, StringSplitOptions.RemoveEmptyEntries);
Another option:
Replace the string delimiter with a single character, then split on that character.
string input = "abc][rfd][5][,][.";
string[] parts1 = input.Replace("][","-").Split('-');
Regex.Split("abc][rfd][5][,][.", #"\]\]");
More fast way using directly a no-string array but a string:
string[] StringSplit(string StringToSplit, string Delimitator)
{
return StringToSplit.Split(new[] { Delimitator }, StringSplitOptions.None);
}
StringSplit("E' una bella giornata oggi", "giornata");
/* Output
[0] "E' una bella giornata"
[1] " oggi"
*/
In .NETCore 2.0 and beyond, there is a Split overload that allows this:
string delimiter = "][";
var results = stringToSplit.Split(delimiter);
Split (netcore 2.0 version)