Sprache: C#
This code split a string in seperate words.
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string s = "This is a big house";
//Split string on spaces.
string[] wordsSplit = s.Split(' ');
foreach (string word in wordsSplit)
{
Console.WriteLine(word);
}
}
}
}
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string s = "This is a big house";
//Split string on spaces.
string[] wordsSplit = s.Split(' ');
foreach (string word in wordsSplit)
{
Console.WriteLine(word);
}
}
}
}
Alte URL:
/snippet/split-string-in-seperate-words/12030
This solution solves this problem not very well. So what is with double spaces (accidently) or tabs and newline between words? Regular Expressions are more recommended on this:
[code]
string words = „Lorem ipsumtdolor sit amet consetetur sadipscing elitr“;
string[] wordsSplit = Regex.Split(words, @“s+“);
[/code]
There are several more problems to solve, e.g. to cope with special characters.
Alternative:
[code]String [] stra = dummy.Split (new char [] { ‚ ‚, ‚;‘, ‚.‘ }, StringSplitOptions.RemoveEmptyEntries);[/code]
@derlucky Wenn du Leistung weg werfen willst, dann nimm Regex. Ansonsten mach es wie @diub und übergib mehrere Split-Zeichen. Da dann ggf. auch den Tabulator usw.
Und wenn du nicht sicher bist, welche WhiteSpaces im Text enthalten sind, verwendest du die folgende Variante.
[code]var words = text.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries)[/code]
Diese Variante trennt nach allen bekannten WhiteSpaces (Link: https://en.wikipedia.org/wiki/Whitespace_character)
@Martin man lernt wirklich nie aus. Danke für den Tipp 🙂
null reicht auch als Parameter anstatt dem char Array, da spart man sich ein wenig tippen.