I have a string in following format
"TestString 1 <^> TestString 2 <^> Test String3
Which i want to split by "<^>" string.
Using following statement it gives the output i want
"TestString 1 <^> TestString 2 <^> Test String3"
.Split("<^>".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
But if my string contains "<" , ">" or "^" anywhere in the text then above split statement will consider that as well
Any idea how to split only for "<^>" string ?
By using ToCharArray you are saying "split on any of these characters"; to split on the sequence "<^>" you must use the overload that accepts a string[]:
string[] parts = yourValue.Split(new string[]{"<^>"}, StringSplitOptions.None);
Or in C# 3:
string[] parts = yourValue.Split(new[]{"<^>"}, StringSplitOptions.None);
Edit: As others pointed already out: String.Split has a good overload for your usecase. The answer below is still correct (as in working), but - not the way to go.
That's because this string.Split overload takes an array of separator chars. Each of them splits the string.
You want: Regex.Split
Regex regex = new Regex(#"<\^>");
string[] substrings = regex.Split("TestString 1 <^> TestString 2 <^> Test String3");
And - a sidenote:
"<^>".ToCharArray()
is really just a fancy way to say
new[]{'<', '^', '>'}
Try another overloaded Split method:
public string[] Split(
string[] separator,
StringSplitOptions options
)
So in you case it may looks like:
var result =
yourString.Split(new string[] {"<^>"},StringSplitOptions.RemoveEmptyEntries);
Hope, this helps.
Related
I have string like so:
"ABCDEF--Split--GHIKLMO--Split--PQRSTUVWXYZ"
what I am trying to do is split this string with --Split-- being the delimiter, I have obviously tried the following:
var array = item.split('--Split--');
but I get this error:
Too many characters in character literal
Is there away to do this?
The problem with your above code is that you are trying to pass in a string as a char instead of using the correct overload:
item.Split(new []{"--Split--"}, StringSplitOptions.None)
MSDN Documentation
You're using the string.Split that uses CHAR as parameter. See that single quote are used to create char type in C#.
You have to use the overload that receives an string array as parameter.
item.split(new string[]{"--Split--"},StringSplitOptions.RemoveEmptyEntries);
The first parameter is an array of strings that you want to split.
The second parameter, the StringSplitOptions has two possible values:
None (will split also empty values), the example below will give you an string array with three elements. The first will be "This is an string test", second wil lbe "I will split by ", and third will be "":
var test = "This is an string test, I will split by ,";
var splittedStrings = test.Split(",", StringSplitOptions.None);
RemoveEmptyEntries, this will remove the empty entries of splitted string. If you take the example above using the RemoveEmptyEntries you will have an array with only two values: "This is an string test" and "I will split by ". The empty string at final will be cutted of.
I have three string called This_is string1 and This is string2 There_is string3
How to split these 3 strings after "This_", "This " "There_) in one condition? basically, i want to split based on first "_" or " " in a string in one condition.
The answer is simple, you can use the String.Split method, specifying the multiple delimiters (in your case the underscore and whitespace):
str.Split(new char[]{'_',' '})
LinQPad result:
If you want to split only the first part, you can use the 2nd overload of String.Split:
str.Split(new char[]{'_',' '}, 2);
and this is the result in LinQPad:
So you want to split only the first part? You can use the overload of String.Split that allows to specify the count and multiple delimiters:
str.Split(new[]{' ', '_'}, 2);
So on the first string you get: "This" + "is string1", similar on the others.
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
I have the following return of a printer:
{Ta005000000000000000000F 00000000000000000I 00000000000000000N 00000000000000000FS 00000000000000000IS 00000000000000000NS 00000000000000000}
Ok, I need to save, in a list, the return in parts.
e.g.
[0] "Ta005000000000000000000F"
[1] "00000000000000000I"
[2] "00000000000000000N"
...
The problem is that the number of characters varies.
A tried to make it going into the 'space', taking the substring, but failed...
Any suggestion?
Use String.Split on a single space, and use StringSplitOptions.RemoveEmptyEntries to make sure that multiple spaces are seen as only one delimiter:
var source = "00000000000000000FS 0000000...etc";
var myArray = source.Split(' ', StringSplitOptions.RemoveEmptyEntries);
#EDIT: An elegant way to get rid of the braces is to include them as separators in the Split (thanks to Joachim Isaksson in the comments):
var myArray = source.Split(new[] {' ', '{', '}'}, StringSplitOptions.RemoveEmptyEntries);
You could use a Regex for this:
string input = "{Ta005000000000000000000F 00000000000000000I 00000000000000000N 00000000000000000FS 00000000000000000IS 00000000000000000NS 00000000000000000}";
IEnumerable<string> matches = Regex.Matches(input, "[0-9a-zA-Z]+").Select(m => m.Value);
You can use string.split to create an array of substrings. Split allows you to specify multiple separator characters and to ignore repeated splits if necessary.
You could use the .Split member of the "String" class and split the parts up to that you want.
Sample would be:
string[] input = {Ta005000000000000000000F 00000000000000000I 00000000000000000N 00000000000000000FS 00000000000000000IS 00000000000000000NS 00000000000000000};
string[] splits = input.Split(' ');
Console.WriteLine(splits[0]); // Ta005000000000000000000F
And so on.
Just off the bat. Without considering the encompassing braces:
string printMsg = "Ta005000000000000000000F 00000000000000000I
00000000000000000N 00000000000000000FS
00000000000000000IS 00000000000000000NS 00000000000000000";
string[] msgs = printMsg.Split(' ').ForEach(s=>s.Trim()).ToArray();
Could work.
i have a string like 123Prefix1pics.zip
i want to split it into 123 Prefix1 pics.zip and store them in different variables
i m trying to do it in c#,.net
jst litle bit confused on how to use split method
splitArray = Regex.Split(subjectString, #"(?<=\p{N})(?=\p{L})");
will work in C# to split in positions between a number (\p{N}) and a letter (\p{L}).
If you also want to split between a letter and a number, use
splitArray = Regex.Split(subjectString, #"(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})");
however, that splits your example too much.
You only want to split that one string? Too easy!
string filename = "123Prefix1pics.zip"
string part1 = "123"
string part2 = "Prefix1"
string part3 = "pics.zip"
Ok this is a joke, but it gives the right answer. Unless you generalise your splitting rule, or provide further examples, we can only guess.
You may be asking to make a string break after a numeral, but again I'm only guessing.
You can start with:
string filename = "123Prefix1pics.zip"
string part1 = filename.Substring(0, 3);
string part2 = filename.Substring(3, 7);
string part3 = filename.Substring(10, 4);
You can also note String.Split() needs a separator argument, like an ; or ,. As you don't have any separator, you can try two approaches:
Make sure all your filenames have same format; this way, you can to use Substring() to break your strings
You can identify a more general pattern, as "numbers, 7 characters plus 4 more characters" and to use a regular expression. This is a more advanced solution and can lead to maintenance problems;
I recommend you to stick with first option.
You can split it like this:
your ip in a string variable like this:
create a char vector
then a string vector
Code:
string theIP="this is string";
char[] separator={' '}; //you can put multiple separators
string[] result = theIP.Split(separator,StringSplitOptions.None);
this means result[0] is "this", result[1] is "is", and so on.
You can find a good tutorial about string splitting here:
http://goldwin-advertising.ro/index.php?option=com_content&view=article&id=10:splitstring&catid=3:howto&Itemid=5
Good luck!
Look like you want to split by fixed size.
So use yourString.Substring(0, 3);