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?
Related
string Idstr="ID03I010102010210AEMPD4677EID03I020102020208L8159734ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201";
string[] stringSeparators = new string[] { "ID03I0" };
string[] result;
result = IdStr.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
This is the result:
result[0]=10102010210AEMPD4677E
result[1]=20102020208L8159734
result[3]=30102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201
Desired result:
result[0]=ID03I010102010210AEMPD4677E
result[1]=ID03I020102020208L8159734
result[3]=ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201
As you can see I want to include delimiter ID03I0 to the elements.
NOTE: I know I can include it by hardcoding it. But that's not the way I want to do it.
result = IdStr.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
.Select(x => stringSeparators[0] + x).ToArray();
This adds the seperator to the beginning at every element within your array.
EDIT: Unfortunately with this approach you are limited to use just one single delimiter. So if you want to add more you´d use Regex instead.
Following Regex pattern should work.
string input = "ID03I010102010210AEMPD4677EID03I020102020208L8159734ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201";
string delimiter = "ID03I0";//Modify it as you need
string pattern = string.Format("(?<=.)(?={0})", delimiter);
string[] result = Regex.Split(input, pattern);
Online Demo
Adapted from this answer.
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
var playerBeginMarker = "splitHere!";
string[] playerInfoSet = endgameStats.Split(playerBeginMarker, StringSplitOptions.None);
I'd like to split the endgameStats string, using that playerBeginMarker as the delimiter but it seems to only accept a char.
Use this .Split() overload:
Console.WriteLine("{0}", "moofoobar".Split(new string[] {"o"}, StringSplitOptions.RemoveEmptyEntries));
Use String.Split Method (String(), StringSplitOptions). You would use a String array with one element within - the string you wish to split on.
So it would be:
String[] output = String.Split(new String[]{"splitHere!"}, StringSplitOptions.None)
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();
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);