Page 1 of 1

I don't understand the use of "split" in C#

PostPosted: 08 Dec 2013, 20:27
by Leeizazombie
I'm making a command that renames the server and this is my attempt:

Code: Select all
if (message == "")
     {
         Help(p);
         return;
     }
     string[] split = message.Trim().Split(' ');
     if (message == "name")
     {
         Server.name = split[0];
     }


Clearly I'm doing something wrong with it at Server.name = split[0];
I want it to work like /server name (message)
I plan to add more controls, that's why.

Re: I don't understand the use of "split" in C#

PostPosted: 08 Dec 2013, 20:28
by Leeizazombie
I just realised that this was off-topic, please move it to Help in coding :O

Re: I don't understand the use of "split" in C#

PostPosted: 08 Dec 2013, 20:29
by lucasds12
I don't think this is the appropriate place to add this thread, you may of put this in the place where you put the code you need help with. :mrgreen:
Edit: Bad Lee, don't post before I do. :L
-Lucas

Re: I don't understand the use of "split" in C#

PostPosted: 08 Dec 2013, 20:30
by Leeizazombie
lucasds12 wrote:I don't think this is the appropriate place to add this thread, you may of put this in the place where you put the code you need help with. :mrgreen:
Edit: Bad Lee, don't post before I do. :L
-Lucas


:P

Re: I don't understand the use of "split" in C#

PostPosted: 08 Dec 2013, 20:35
by lucasds12
It looks like this code hasn't been made yet, it seems like it to me, because this is not a normal code line, from what I seen.
And, one day I'll post before you! ;)
-Lucas

Re: I don't understand the use of "split" in C#

PostPosted: 09 Dec 2013, 01:46
by Warren1001
Explain what you expect your piece of code to do?

Re: I don't understand the use of "split" in C#

PostPosted: 09 Dec 2013, 05:02
by ismellike
This is how split works.

message.Split(' '); splits your message by the spaces, so let's take your example: "/server name message"

/server is the command so that's not actually part of the message...

We are left with "name message"

split[0] is the first split, name.
split[1] is the second part, message.

You need to change your 2nd if statement to if(split[0] == "name"), and server.name has to equal split[1].

I'm not sure if that actually changes the server name though.

Re: I don't understand the use of "split" in C#

PostPosted: 09 Dec 2013, 16:26
by joppiesaus
ismellike wrote:This is how split works.

message.Split(' '); splits your message by the spaces, so let's take your example: "/server name message"

/server is the command so that's not actually part of the message...

We are left with "name message"

split[0] is the first split, name.
split[1] is the second part, message.

You need to change your 2nd if statement to if(split[0] == "name"), and server.name has to equal split[1].

I'm not sure if that actually changes the server name though.


What you said.

Here's some extra info of Arrays, which makes you hopefully understand more the split[0] stuff.