xxxxxxxxxx
//You can also split by a string. This is in .NET 6, so make sure your up to date, but it is helpful
//when your split scope is greater than just a single character.
string sentence = "Learning never exhausts the mind. - Leonardo da Vinci"
//Remember that the split() function returns an array of strings based on how
//many hits it finds based on the delimiter provided.
var splitString = sentence.Split("never", 2, StringSplitOptions.None);
//Output: splitString[0] = "Learning" splitString[1] = "exhausts the mind. - Leonardo da Vinci"
//The number 2 parameter in that split function is hardcoding how many substrings
//you want to return from the split function.
//https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=net-6.0#system-string-split(system-string()-system-int32-system-stringsplitoptions)
//If you are using a .NET version that is older than version 6 use Regex instead.
var splitString = Regex.Split(sentence, "never");
xxxxxxxxxx
// To split a string use 'Split()', you can choose where to split
string text = "Hello World!"
string[] textSplit = text.Split(" ");
// Output:
// ["Hello", "World!"]
xxxxxxxxxx
string input = "Hello,World,How,Are,You?";
char[] delimiter = {','};
string[] stringArray = input.Split(delimiter);
// The stringArray will contain: ["Hello", "World", "How", "Are", "You?"]
xxxxxxxxxx
string sentence = "The quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(new char[] {' ', '\t', '\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
This will split the sentence string by all whitespace characters (space, tab, newline, and carriage return) and remove any empty entries in the resulting array. The resulting words array will contain:
["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
xxxxxxxxxx
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiterChars);
System.Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}
xxxxxxxxxx
var lines = input
.ReplaceLineEndings()
.Split(Environment.NewLine, StringSplitOptions.None);
xxxxxxxxxx
string test = "aaa=bbb&aaa2=xxx"
string[] spiltChar = test.Split(new Char[] { '&' });
xxxxxxxxxx
static IEnumerable<string> Split(string str, int chunkSize)
{
return Enumerable.Range(0, str.Length / chunkSize)
.Select(i => str.Substring(i * chunkSize, chunkSize));
}