C
C#โ€ข13mo 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
AdiZOPโ€ข13mo ago
Please ping if you reply ๐Ÿ™‚
Angius
Angiusโ€ข13mo ago
Split it, then serialize it as JSON and you'll get the desired format
AdiZ
AdiZOPโ€ข13mo ago
Serializing as JSON = something I've never heard of. Is it easy to figure out from a quick Google?
Angius
Angiusโ€ข13mo ago
using System.Text.Json namespace, JsonSerializer.Serialize(the_data) Ez
AdiZ
AdiZOPโ€ข13mo ago
And where does it get stored?
Angius
Angiusโ€ข13mo ago
In... the variable you assign it to? It returns a string Formatted as Json How you handle it is up to you
AdiZ
AdiZOPโ€ข13mo 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โ€ข13mo ago
Json files are commonly used to store data, so that's probably why But serialization alone will only return a string
AdiZ
AdiZOPโ€ข13mo ago
Ok so that worked excellently, but I'm having a weird issue with .Split().
AdiZ
AdiZOPโ€ข13mo 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
AdiZOPโ€ข13mo 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โ€ข13mo ago
What's your full code? The splitting and all
AdiZ
AdiZOPโ€ข13mo 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โ€ข13mo 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
AdiZOPโ€ข13mo ago
huh That's very weird
AdiZ
AdiZOPโ€ข13mo ago
AdiZ
AdiZOPโ€ข13mo ago
This is the full code that I have.
Angius
Angiusโ€ข13mo ago
Could be a system-dependent issue
AdiZ
AdiZOPโ€ข13mo ago
Oh nvm it caps out anyway Just installed C# on VSCode
Angius
Angiusโ€ข13mo 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
AdiZOPโ€ข13mo ago
So do I just change it to .Split('\n')?
Angius
Angiusโ€ข13mo ago
You could always filter the strings Or split exactly by \n or by "\n", "\n\r"
AdiZ
AdiZOPโ€ข13mo 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โ€ข13mo ago
Yeah, so try .Split("\n", "\n\r")
AdiZ
AdiZOPโ€ข13mo 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โ€ข13mo ago
Now you'll have issues on systems that don't use carriage return ๐Ÿ˜›
AdiZ
AdiZOPโ€ข13mo 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โ€ข13mo ago
Split by \n and .Trim() each element, that'd probably be the easiest and most x-plat way
AdiZ
AdiZOPโ€ข13mo ago
But I feel like I'm initializing that string[] wrong
Angius
Angiusโ€ข13mo ago
Uh, yes That's not how you initialize an array
AdiZ
AdiZOPโ€ข13mo ago
Sorry I shouldn't be asking here I can definitely find this in the docs
Angius
Angiusโ€ข13mo ago
string[] foo = new[]{ "abc", "def", "ghi" };
string[] foo = new[]{ "abc", "def", "ghi" };
AdiZ
AdiZOPโ€ข13mo ago
Oh you have to initialize it first? That can't be right
Angius
Angiusโ€ข13mo ago
Could even omit new[] really
AdiZ
AdiZOPโ€ข13mo ago
Yeah ok never mind How are you using {}? I thought it had to be []?
Angius
Angiusโ€ข13mo 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
AdiZOPโ€ข13mo ago
Ah that's interesting Got it
Angius
Angiusโ€ข13mo 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
AdiZOPโ€ข13mo ago
Ohhh I was wondering How many dimensions can an array have?
Angius
Angiusโ€ข13mo 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
AdiZOPโ€ข13mo 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