C
C#โ€ข7mo ago
AdiZ

โœ… .Split() Returning Spaces Between Words in String[]

Hi. I have an string array string[] strArr of words in format [word1, word2] and so on - just a basic format.. I got that by performing .Split() on another string, let's say string x. x was in format
"""
word1
word2
"""
"""
word1
word2
"""
and so on, for 30,000 lines. Now that I've converted this all to an array, how can I print the array such that the Console output would be ["word1", "word2"] etc.? I want to initialize strArr instead of performing .Split() everytime my app starts.
41 Replies
AdiZ
AdiZโ€ข7mo ago
Please ping if you reply ๐Ÿ™‚
Angius
Angiusโ€ข7mo ago
Split it, then serialize it as JSON and you'll get the desired format
AdiZ
AdiZโ€ข7mo ago
Serializing as JSON = something I've never heard of. Is it easy to figure out from a quick Google?
Angius
Angiusโ€ข7mo ago
using System.Text.Json namespace, JsonSerializer.Serialize(the_data) Ez
AdiZ
AdiZโ€ข7mo ago
And where does it get stored?
Angius
Angiusโ€ข7mo ago
In... the variable you assign it to? It returns a string Formatted as Json How you handle it is up to you
AdiZ
AdiZโ€ข7mo ago
Ok cool I'll go do that real quick Was looking at an instructional page and it was talking about opening writing and closing files so I was thinking of that ๐Ÿ˜
Angius
Angiusโ€ข7mo ago
Json files are commonly used to store data, so that's probably why But serialization alone will only return a string
AdiZ
AdiZโ€ข7mo ago
Ok so that worked excellently, but I'm having a weird issue with .Split().
AdiZ
AdiZโ€ข7mo ago
GitHub
google-10000-english/google-10000-english.txt at master ยท first20ho...
This repo contains a list of the 10,000 most common English words in order of frequency, as determined by n-gram frequency analysis of the Google's Trillion Word Corpus. - first20hours/goog...
AdiZ
AdiZโ€ข7mo ago
Grabbing from this list, I'm just using .Split() to separate into a string[] of all the words as they're just separated by lines. But I'm getting "keen","","flyer","","peas","","dosage","","receivers", i.e. with spaces in between. Why could this be happening?
Angius
Angiusโ€ข7mo ago
What's your full code? The splitting and all
AdiZ
AdiZโ€ข7mo ago
using System.Text.Json;

string originalStr = """
arbor
mediawiki
configurations
poison
""";
Console.WriteLine(JsonSerializer.Serialize(originalStr.Split()));
using System.Text.Json;

string originalStr = """
arbor
mediawiki
configurations
poison
""";
Console.WriteLine(JsonSerializer.Serialize(originalStr.Split()));
Obviously originalStr here is a sample size, but I used all 10k words for the real thing (though that doesn't matter). Let's actually try this
MODiX
MODiXโ€ข7mo ago
AdiZ
REPL Result: Success
using System.Text.Json;

string originalStr = """
arbor
mediawiki
configurations
poison
""";
Console.WriteLine(JsonSerializer.Serialize(originalStr.Split()));
using System.Text.Json;

string originalStr = """
arbor
mediawiki
configurations
poison
""";
Console.WriteLine(JsonSerializer.Serialize(originalStr.Split()));
Console Output
["arbor","mediawiki","configurations","poison"]
["arbor","mediawiki","configurations","poison"]
Compile: 524.066ms | Execution: 72.907ms | React with โŒ to remove this embed.
AdiZ
AdiZโ€ข7mo ago
huh That's very weird
AdiZ
AdiZโ€ข7mo ago
AdiZ
AdiZโ€ข7mo ago
This is the full code that I have.
Angius
Angiusโ€ข7mo ago
Could be a system-dependent issue
AdiZ
AdiZโ€ข7mo ago
Oh nvm it caps out anyway Just installed C# on VSCode
Angius
Angiusโ€ข7mo ago
Linux โ€” which the bot runs on โ€” uses \n for line ends Windows uses \n\r Idk ยฏ\_(ใƒ„)_/ยฏ Oh Right And both are whitespace, so that's what .Split() splits on by default word\n\r gets split by whitespace into word, empty string
AdiZ
AdiZโ€ข7mo ago
So do I just change it to .Split('\n')?
Angius
Angiusโ€ข7mo ago
You could always filter the strings Or split exactly by \n or by "\n", "\n\r"
AdiZ
AdiZโ€ข7mo ago
Interesting... "liechtenstein\r","mating\r","compute\r","redhead\r","arrives\r" is what I get when I do
originalStr.Split("\n")
originalStr.Split("\n")
Angius
Angiusโ€ข7mo ago
Yeah, so try .Split("\n", "\n\r")
AdiZ
AdiZโ€ข7mo ago
Are you sure? Just throws error CS1503: Argument 1: cannot convert from 'string' to 'char' Argument 2: cannot convert from 'string' to 'int' I did exactly this originalStr.Split("\r\n") This worked perfectly. I don't claim to have a clue what's going on Ohh carriage returns I've done those before Makes sense Thanks @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angiusโ€ข7mo ago
Now you'll have issues on systems that don't use carriage return ๐Ÿ˜›
AdiZ
AdiZโ€ข7mo ago
Ah well I got my JSON, that's all I care about xD What would I do if I couldn't use \r? @ZZZZZZZZZZZZZZZZZZZZZZZZZ Now I've got string[] tenKMostCommonWords = new String["the","of","and","to"..."poison"]; And on the character before the " at the start of poison, Unity gives me Assets\Scripts\ListConstants.cs(7,95926): error CS0029: Cannot implicitly convert type 'string' to 'int'
Angius
Angiusโ€ข7mo ago
Split by \n and .Trim() each element, that'd probably be the easiest and most x-plat way
AdiZ
AdiZโ€ข7mo ago
But I feel like I'm initializing that string[] wrong
Angius
Angiusโ€ข7mo ago
Uh, yes That's not how you initialize an array
AdiZ
AdiZโ€ข7mo ago
Sorry I shouldn't be asking here I can definitely find this in the docs
Angius
Angiusโ€ข7mo ago
string[] foo = new[]{ "abc", "def", "ghi" };
string[] foo = new[]{ "abc", "def", "ghi" };
AdiZ
AdiZโ€ข7mo ago
Oh you have to initialize it first? That can't be right
Angius
Angiusโ€ข7mo ago
Could even omit new[] really
AdiZ
AdiZโ€ข7mo ago
Yeah ok never mind How are you using {}? I thought it had to be []?
Angius
Angiusโ€ข7mo ago
Nope [] denotes the array It can contain an integer for size var foo = new int[6]() will create a new int array of size 6 for example
AdiZ
AdiZโ€ข7mo ago
Ah that's interesting Got it
Angius
Angiusโ€ข7mo ago
var foo = new int[6, 7](); will make a new 2D array of size 6x7 That's why it was complaining about not being able to treat a string as an int
AdiZ
AdiZโ€ข7mo ago
Ohhh I was wondering How many dimensions can an array have?
Angius
Angiusโ€ข7mo ago
For initialization, you use {}
var person = new Person {
Name = "Bob",
Age = 67
};
var data = new Dictionary<string, int>() {
["one"] = 1,
["two"] = 2,
};
var person = new Person {
Name = "Bob",
Age = 67
};
var data = new Dictionary<string, int>() {
["one"] = 1,
["two"] = 2,
};
etc Not sure if there's a limit There probably is, but something like 256 or some such
AdiZ
AdiZโ€ข7mo ago
Apparently 32 Makes sense 32-bit Alright thank you so much mate I think you've answered every question I've asked here to date What would I do without ya ๐Ÿคฃ Much appreciated ๐Ÿ‘
Want results from more Discord servers?
Add your server
More Posts