โ .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
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
Please ping if you reply ๐
Split it, then serialize it as JSON and you'll get the desired format
Serializing as JSON = something I've never heard of. Is it easy to figure out from a quick Google?
using System.Text.Json
namespace, JsonSerializer.Serialize(the_data)
EzAnd where does it get stored?
In... the variable you assign it to?
It returns a string
Formatted as Json
How you handle it is up to you
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 ๐
Json files are commonly used to store data, so that's probably why
But serialization alone will only return a string
Ok so that worked excellently, but I'm having a weird issue with
.Split()
.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...
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?What's your full code?
The splitting and all
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 thisAdiZ
REPL Result: Success
Console Output
Compile: 524.066ms | Execution: 72.907ms | React with โ to remove this embed.
huh
That's very weird
This is the full code that I have.
Could be a system-dependent issue
Oh nvm it caps out anyway
Just installed C# on VSCode
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
So do I just change it to
.Split('\n')
?You could always filter the strings
Or split exactly by
\n
or by "\n", "\n\r"
Interesting...
"liechtenstein\r","mating\r","compute\r","redhead\r","arrives\r"
is what I get when I do Yeah, so try
.Split("\n", "\n\r")
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 @ZZZZZZZZZZZZZZZZZZZZZZZZZNow you'll have issues on systems that don't use carriage return ๐
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'
Split by
\n
and .Trim()
each element, that'd probably be the easiest and most x-plat wayBut I feel like I'm initializing that
string[]
wrongUh, yes
That's not how you initialize an array
Sorry I shouldn't be asking here I can definitely find this in the docs
Oh you have to initialize it first? That can't be right
Could even omit
new[]
reallyYeah ok never mind
How are you using
{}
? I thought it had to be []
?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 exampleAh that's interesting
Got it
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 intOhhh
I was wondering
How many dimensions can an array have?
For initialization, you use
{}
etc
Not sure if there's a limit
There probably is, but something like 256 or some suchApparently 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 ๐