Question

I am having trouble with something that seems so simple. How do i get a sentence from a command?

If a user entered : /command i want to get this with spaces minus the command and pressed enter, i would need to get this :

myString = "i want to get this with spaces minus the command";

How would i do this? A loop? And what would the the fastest, most efficient way to do this?

Thanks. This is what i have so far:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("command"))
    {   
        if(args.length > 0)
        {
            sender.sendMessage(ChatColor.RED + "/command <message>");
        }
        else
        {
            if (!(sender instanceof Player)) {
                sender.sendMessage("This command can only be run by a player.");
            } 
            else 
            {
                Player player = (Player) sender;
                // Check to make sure nothing is null or empty
                if(args[0].equals(" ") || args[0].equals("") || args[0] == null)
                {
                    // Do command !!
                }
                else
                {
                    player.sendMessage(ChatColor.RED + "Please enter a <message>");
                }
            }
        }
        return true;
    }
    return false;
}
Was it helpful?

Solution

you could do this easily by using a for loop to get the all of the strings after the /command, which all are processed as arguments. Here's an example:

String myString = ""; //we're going to store the arguments here    

for(int i = 0; i < args.length; i++){ //loop threw all the arguments
    String arg = args[i] + " "; //get the argument, and add a space so that the words get spaced out
    myString = myString + arg; //add the argument to myString
}

Now you have your string, and you could do anything with it, like send the message to the command sender:

sender.sendMessage(myString);

A short explanation of the code above is, first, we're looping threw all of the arguments (everything that comes after the /command, then, we're adding a space to the end of the argument, and last, we put the argument into the string, myString... Here's an example of implementation:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {       
    if(cmd.getName().equalsIgnoreCase("command")){
        String myString = "";

        for(int i = 0; i < args.length(); i++){
            String arg = args[i] + " ";
            myString = myString + arg;
        }

        sender.sendMessage(myString); //send the message to the command sender.
    }
}

you could also check if the command has arguments using:

if(args.length != 0){

An example when using a command is, let's say a player types /command bar foo. The player would then get sent the message bar foo.

OTHER TIPS

Code:

String myString = "/command this is the text we want to grab right here";  
myString = myString.subStr( myString.indexOf( ' ' ) + 1);  

Explanation:

So if you have a String myString = "/command this is what we want", then you can seperate that first word by using .subStr(). subStr() has 2 versions, one with one parameter which is the starting point and gives you the string to the end, and another with two parameters which are the starting point and ending point and that gives you the string in between those locations.

Since we are getting the string from a certain position to the end, we are using the first method with one parameter. What will that parameter be? Well we know that the first character we want is right after the space after /command. So it is the location one after first space in the line. How do we get the location of the first space? Easy, String has a built in method indexOf() which can take in a String or a char and will return the location of the first occurrence of what we passed in.

We will pass in the char ' ' to indexOf(char c) since we want to find the location of the first space. Because once we know the location of the first space, we can go one after that and have the location of the start of what we need. Then we will pass that to subStr(int location) to get the string from that location to the end, and that is what we want.

So all of this sounds very complicated but the code is self explanatory:

String myString = "/command this is the text we want to grab right here";  
myString = myString.subStr( myString.indexOf( ' ' ) + 1);

You could most easily do this with a String Builder.

public String getArgs(String[] args, int num){ //You can use a method if you want
    StringBuilder sb = new StringBuilder(); //We make a String Builder
    String prefix = ""; //We're going to store the space here
    for(int i = num; i < args.length; i++) { //We get all the arguments with a for loop
        sb.append(prefix); //We apply the space
        prefix = " "; //We save the space in our prefix string
        sb.append(args[i]); //We add the argument
    }
    return sb.toString(); //We return the message
}

or

public String getArgs(String[] args, int num){ //You can use a method if you want
    StringBuilder sb = new StringBuilder(); //We make a String Builder
    for(int i = num; i < args.length; i++) { //We get all the arguments with a for loop
        sb.append(args[i]).append(" "); //We add the argument and the space
    }
    return sb.toString().trim(); //We return the message
}

I like this second method the most :3

The "int i = num" is the number of the argument we want to start to get them all, eg: You have a command like this: /announce add message (message).

The message starts at args[2] so we'll do: getArgs(args, 2)

If you want to have one-word arguments before you have a multi-word argument you would want to loop through each argument starting at a certain one.

for(int i = 1; args.length > 1; i++){
    String argsString = args[i] + " ";
}

To take all of the arguments given as one you just do this:

String argsString = args.toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top