Split a String by Another String in C#

Split a string by another string in C#

In order to split by a string you'll have to use the string array overload.

string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);

How to split string by another string

You can use Regex.Split for accomplishing this

string splitStr = "|", inputStr = "ISA~this is date~ESA|ISA~this is more data~ESA|";

var regex = new Regex($@"(?<=ESA){Regex.Escape(splitStr)}(?=ISA)", RegexOptions.Compiled);
var items = regex.Split(inputStr);

foreach (var item in items) {
Console.WriteLine(item);
}

Output:

ISA~this is date~ESA
ISA~this is more data~ESA|

Note that if your string between the ISA and ESA have the same pattern that we are looking for, then you will have to find some smart way around it.

To explain the Regex a bit:

(?<=ESA)   Look-behind assertion. This portion is not captured but still matched
(?=ISA) Look-ahead assertion. This portion is not captured but still matched

Using these look-around assertions you can find the correct | character for splitting

c# Split string using another string as delimiter and include delimiter as part of the splitted string

You may use a positive lookahead based solution:

var result = Regex.Split(content, $@"(?={string.Join("|", delimiters.Select(m => Regex.Escape(m)))})")
.Where(x => !string.IsNullOrEmpty(x))

See the C# demo:

var content="heading1: contents with respect to heading1 heading2: heading2 contents heading3: heading 3 related contents sample strings";
var delimiters = new string[] {"heading1:","heading2:","heading3:"};
Console.WriteLine(
string.Join("\n",
Regex.Split(content, $@"(?={string.Join("|", delimiters.Select(m => Regex.Escape(m)))})")
.Where(x => !string.IsNullOrEmpty(x))
)
);

Output:

heading1: contents with respect to heading1 
heading2: heading2 contents
heading3: heading 3 related contents sample strings

The (?={string.Join("|", delimiters.Select(m => Regex.Escape(m)))}) will construct a regex dynamically, it will look like

(?=heading1:|heading2:|heading3:)

See the regex demo. The pattern will basically match any position in the string that is followed with either herring1:, herring2: or herring3: without consuming these substrings, so they will land in the output.

Note that delimiters.Select(m => Regex.Escape(m)) is there to make sure all special regex metacharacters that might be in the delimiters are escaped and treated as literal chars by the regex engine.

Split string into two separate strings

Use split()

    string name= "LUKE CARROLL";
string[] tmp = name.Split(' ');
string FirstName = tmp [0];
string LastName = tmp [1];

Split a string by another string except by

If I have understood your question, the below sample will give the intended output :)

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Xunit;

namespace XUnitTestProject1
{
public class UnitTest1
{
[Fact]
public void TestPatternSplit()
{

var input = @"AV Rocket 456:Contact?:Jane or Tarzan:URL?:http?://www.jane.com:Time Delivered 18?:15:Product Description";

var output = PatternSplit(input);

var expected = new[]{"AV Rocket 456", "Contact?:Jane or Tarzan", "URL?:http?://www.jane.com","Time Delivered 18?:15","Product Description"};

Assert.Equal(expected, output);

}

private static IEnumerable<string> PatternSplit(string input)
{
const string pattern = @"(?<!\?):";
return Regex.Split(input, pattern);
}
}
}

How can I split a string with a string delimiter?


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(',');

How do I split a string by a multi-character delimiter in C#?

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Example from the docs:

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;

// ...
result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}

c# splitting strings using string instead of char as seperator

Use String.Split Method (String[], StringSplitOptions) to split it like:

string[] sentences = text.Split(new string[] { ". " },StringSplitOptions.None);

You will end up with two items in your string:

1.2788923 is a decimal number
1243818 is an integer

Split string and get a substring of each element


text.Split('|').Select(s => s.Split(':').Last()).ToArray()

How to split a defined string from another string and get the first item after split


var result = fullpath.Replace(samplePath, "").Split('\\')[1];

You can replace the first part (samplePath) with nothing, removing it (or you could use Substring to get the second part of the fullPath, counting the characters of samplePath), and then Split the result on '\', getting the second occurrence, which is the result you expect.

Here's a working version: https://dotnetfiddle.net/k4tfGP



Related Topics



Leave a reply



Submit