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