Spacing etc help

Spacing etc help

Postby Warren1001 » 21 Jul 2013, 18:13

Working on new tempban that allows you to put a reason for kick (and logs the ban)
I've got the .IndexOf etc messed up, and I want to know how to get it right, cuz its a bit confusing.
Code: Select all
            if (message.Split(' ').Length > 2) // if someone is going to provide a reason.
            {
                m = message.Substring(message.IndexOf(' ') + 2); // there is where im having trouble.
                if (p == null)
                {
                    File.AppendAllText(path, Environment.NewLine + who.name + " - temp-banned by: Console/IRC, for: " + minutes + " minute(s), reason: " + m + ", date: " + DateTime.Now + ".");
                }
                File.AppendAllText(path, Environment.NewLine + who.name + " - temp-banned by: " + p.PublicName + ", for: " + minutes + " minute(s), reason: " + m + ", date: " + DateTime.Now + ".");
            }
            else // if no reason
            {

Format is as is, /tempban [player] [time] [reason]
When I type /tempban player time, it says invalid minutes
same with if I put a reason, I'm typing a number between 1 and 60
/e string m represents the kick message
Halp?:3
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: Spacing etc help

Postby Conor » 22 Jul 2013, 06:47

This example might help you with your splitting.

Code: Select all
string message = "msg1 msg2 hello there my friend.";
string[] split = message.Split(new char[] { ' ' }, 3);


split[0] would = "msg1".
split[1] would = "msg2".
split[2] would = "hello there my friend.".

To get the text after the second space using the Substring method just gets too messy. This is how you can do it though.
Code: Select all
string message = "msg1 msg2 hello there my friend.";
string helloThere = message.Substring(message.IndexOf(' ') + 1).Substring(message.Substring(message.IndexOf(' ') + 1).IndexOf(' ') + 1);

helloThere would = "hello there my friend.".
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Spacing etc help

Postby Warren1001 » 22 Jul 2013, 07:13

I still get the invalid minutes...
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: Spacing etc help

Postby Warren1001 » 22 Jul 2013, 07:14

Code: Select all
            int minutes;
            try
            {
                minutes = int.Parse(message.Split(' ')[1]);
            }
            catch
            {
                Player.SendMessage(p, "Invalid minutes.");
                return;
            }
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: Spacing etc help

Postby Warren1001 » 22 Jul 2013, 21:00

Nevermind, apparently I didn't recompile xD
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 1 guest

cron