C
C#13mo ago
AdiZ

✅ Split Lines into Array Elements

I've got a list of common English words in the format
word1
word2
word3
word4
word1
word2
word3
word4
And so forth. How can I split this into an array of format `string[] arr = {"word1", "word2", "word3", "word4"}. I imagine this would be done with .Split() but it's not working for me.
21 Replies
SinFluxx
SinFluxx13mo ago
When you say it's in that format, you mean it's a load of words, 1 per line, in some file and you're reading them in?
alex
alex13mo ago
what have u tried, what error did u encounter?
TheRanger
TheRanger13mo ago
.Split definitely works u are probably using it wrong
nohopestage
nohopestage13mo ago
By default Split uses a whitespace as a delimiter. Pass Environment.NewLine as an argument Assuming each word is split by a new line
cap5lut
cap5lut13mo ago
Environment.NewLine is usually not enough as different operating systems/applications/user settings can affect the actual new line sequence
MODiX
MODiX13mo ago
AdiZ
REPL Result: Failure
string s =
"
abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
string s =
"
abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
Exception: CompilationErrorException
- Newline in constant
- ; expected
- ; expected
- ; expected
- Newline in constant
- ; expected
- The type or namespace name 'abc' could not be found (are you missing a using directive or an assembly reference?)
- The type or namespace name 'ghi' could not be found (are you missing a using directive or an assembly reference?)
- Newline in constant
- ; expected
- ; expected
- ; expected
- Newline in constant
- ; expected
- The type or namespace name 'abc' could not be found (are you missing a using directive or an assembly reference?)
- The type or namespace name 'ghi' could not be found (are you missing a using directive or an assembly reference?)
Compile: 699.045ms | Execution: 0.000ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
AdiZ
REPL Result: Failure
string s = "abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
string s = "abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
Exception: CompilationErrorException
- Newline in constant
- ; expected
- ; expected
- ; expected
- Newline in constant
- ; expected
- The type or namespace name 'def' could not be found (are you missing a using directive or an assembly reference?)
- The name 'jkl' does not exist in the current context
- Newline in constant
- ; expected
- ; expected
- ; expected
- Newline in constant
- ; expected
- The type or namespace name 'def' could not be found (are you missing a using directive or an assembly reference?)
- The name 'jkl' does not exist in the current context
Compile: 501.971ms | Execution: 0.000ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
AdiZ
REPL Result: Success
string s = "abc\ndef\nghi\njkl\n";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
string s = "abc\ndef\nghi\njkl\n";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
Console Output
abc
def
ghi
jkl
abc
def
ghi
jkl
Compile: 611.380ms | Execution: 47.155ms | React with ❌ to remove this embed.
AdiZ
AdiZOP13mo ago
Oh My bad guys.
alex
alex13mo ago
ngl i didnt know it splits on newlines by default
PixxelKick
PixxelKick13mo ago
If you are reading this from a file, btw, there's a handy existing method you can use that will handle this for you. File.ReadLinesAsync(string path) returns an async enumerable of strings, 1 per line https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlinesasync?view=net-7.0
File.ReadLinesAsync Method (System.IO)
Asynchronously reads the lines of a file.
PixxelKick
PixxelKick13mo ago
You will want to use await foreach for the IAsyncEnumerable it returns, if you dont want to dig into learning how to use await foreach though you can use the synchronous version of the method, File.ReadLines(string path)
Jimmacle
Jimmacle13mo ago
triple quotes for multiline string literals, fyi
var x = """
my string
""";
var x = """
my string
""";
MODiX
MODiX13mo ago
TheRanger
REPL Result: Success
string s = @"abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
string s = @"abc
def
ghi
jkl
";

foreach (string word in s.Split())
{
Console.WriteLine(word);
}
Console Output
abc
def
ghi
jkl
abc
def
ghi
jkl
Compile: 614.818ms | Execution: 91.826ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle13mo ago
or that
TheRanger
TheRanger13mo ago
wait why """ exists if @ exists? 🤔
Jimmacle
Jimmacle13mo ago
triple quote version is a lot nicer when you have indentation because it aligns the "base" indentation of the string with the indentation of the end """
MODiX
MODiX13mo ago
Jimmacle
REPL Result: Success
Console.WriteLine("""
super indented
""")
Console.WriteLine("""
super indented
""")
Console Output
super indented
super indented
Compile: 511.960ms | Execution: 81.608ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
TheRanger
REPL Result: Success
Console.WriteLine(@"
super nintendo
")
Console.WriteLine(@"
super nintendo
")
Console Output
super nintendo

super nintendo

Compile: 564.989ms | Execution: 38.691ms | React with ❌ to remove this embed.
TheRanger
TheRanger13mo ago
ah
cap5lut
cap5lut13mo ago
well, thats just partially correct
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
line breaks just are whitespaces as well, just like space and tab
Want results from more Discord servers?
Add your server