C
C#

✅ .Split() Returning Spaces Between Words in String[]

✅ .Split() Returning Spaces Between Words in String[]

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

Looking for more? Join the community!

C
C#

✅ .Split() Returning Spaces Between Words in String[]

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
I want to compile a program myself with less filesI was working on a open source project, and I want to compile the program myself so I setup a github✅ Finding Processes but it displays as invisibleSo I am making a system monitor, and I ran into a problem where when I click the button to display t✅ Getting artifact from GitHub Repo ends in SSL_ERROR_SSLHello guys, working on a console app to more or less download artifacts from our private GitHub Repo✅ DB ErrorHey guys, I am working on a project and I am trying to connect it to a DB. I am following the instruCalculate Field Offset Based On Members' NameI'd like to know if the offset calculation is correct. the input used is as follows```cs private coFollowing a video to create power ups, but its not picking them up and I dont know why.Heres the code. I dont know whats wrong with it, from what I can tell its exactly the same as the vConverting int to double to output with decimalsHello again! i got a little problem with converting int to double, i dont get it to output the decimDb ConnectionGood afternoon guys, I need some help setting up the database connection for my project. I have thCan't read property in code behind```cs @page "/weather" @attribute [StreamRendering] <PageTitle>Weather</PageTitle> <h1>Weather</h1RabbitMQ starting service issuePlease guys I had `RabbitMQ` running fine, but after restarting my computer the RabbitMQ won't be stException Unhandled : Could not load file or assembly 'Serilog.Extensions.HostingWhen I run my application, this exception is thrown even before the start of debugging. System.IO.Figot this code that i wrote the get all score and print end score but i didnt read right help meexem is 40% assigments is 30% and participation is 30%Assistance regarding deciding on what to use.Hello there, I want to port one of my macOS apps to a native windows app and would like to ask which✅ C# SMS/MMS BotI have a project I'm working on I have a working PBX that receives SMS and MMS messages to a web conHelp deciding Next.js/Node.js vs .NET API back-endHello. I really want to use .NET as my API back-end for a practice app I'm building. However, I dohow do i make it so when i press it this happens and if i do it again another thing happensive made a code in winforms so when i press with my left mouse button it hides everything, how do i Can't access object attributes using X.PagedListI have a search field that returns a list of movies. It works fine if I reference the model as @MoviTryin to install scriptscs but cinst is not a recognized commandI'm trying to install scriptcs but cinst isn't a recognized command. I have uninstalled and reinstalHow do I utilize Identity Framework and AspNetUser Table?Hello, I am building an MVC app where people will need to sign up. They can then upload stuff to th✅ Is there a way to run a commands method inside of another command? (DSharpPlus)Title explains it. ```c# #region RandomImage [SlashCommand("randomimage", "Visa en r