sum1
sum1

Reputation: 83

Split text with string

i have a text like. on day.Momnday vhfjj j gjhgjh ghghjg hjgh jghj gug on day.tuesday bhjgghkg hjkhjkg jkghkj on day.wednesday ghjgjh jghhgihi juhihi hji on day.Friday jkhkj hjkhk j hjkh kj

now i want this text to store into array like on day.monday remianing text on day.tuesday remaning text and so on. i tried

MyText.Split(new string[] { "on day." }, StringSplitOptions.None);
Regex.Split(MyText, "on day.");

but both return only 1 result.text can contain whitespaces more than once, only idea we have to make sentance is starting sentance with "on day."

Upvotes: 0

Views: 100

Answers (2)

JohnyL
JohnyL

Reputation: 7122

As @DisplayName noticed, Split method for .NET Framework doesn't have overload accepting string parameter as separator. It exists only in .NET Core. So, there will be two solutions:

string input = @"on day.Monday vhfjj ... hji on day.Friday jkhkj hjkhk j hjkh kj";

.NET Core

string[] x = input
             .Split("on day.", StringSplitOptions.RemoveEmptyEntries)
             .Select(z => "on day." + z)
             .ToArray();

.NET Framework

string[] x = Regex
             .Split(input, "on day.")
             .Select(z => "on day." + z)
             .Skip(1) //Emulation of StringSplitOptions.RemoveEmptyEntries
             .ToArray();

Upvotes: 2

DisplayName
DisplayName

Reputation: 13386

if you want to keep the "on day." separator you may use:

        string MyText = "on day.Momnday vhfjj j gjhgjh ghghjg hjgh jghj gug on day.tuesday bhjgghkg hjkhjkg jkghkj on day.wednesday ghjgjh jghhgihi juhihi hji on day.Friday jkhkj hjkhk j hjkh kj";
        MyText=MyText.Replace("on day.", "on day|on day.");
        string[] days = MyText.Split(new string[] { "on day|" }, StringSplitOptions.None);

Upvotes: 2

Related Questions