从java中带有符号的字符串数组返回单词?

时间:2021-11-29 21:17:09
public String onGoogleCommand(String[] args) {
    if(args.length == 0){
        return "Type in a question after the google command!";
    }
    if(args.length >= 1){
        return "https://www.google.com/#q=" + args[0] + "+" + args[1] + "+" + args[2];
    }
    return "What?";
}

What I am asking about is the part where I say return "https://www.google.com/#q=" + args[0] + "+" + args[1] + "+" + args[2];. Obviously, this probably isn't the best way to code a search function, but how can I automate this so that the words from the String[] args automatically get put into my return statement with "+" between each of the words so that it would return something like https://www.google.com/#q=please+help+me+with+this+question?

我要问的是返回https://www.google.com/#q=" + args[0] +" +" + args[1] +" +" +" args[2];显然,这可能不是编写搜索函数的最佳方式,但我如何能够自动执行,以便字符串[]args中的单词自动被放入我的返回语句中,每个单词之间都带有“+”,这样它就会返回https://www.google.com/#q= +help+我+这个+问题?

5 个解决方案

#1


3  

By using Arrays.toString and replace you can achieve the result you want

通过使用数组。toString和replace可以实现您想要的结果

String array[] = {"please", "help", "me"};
String output = "https://www.google.com/#q=" + Arrays.toString(array).
                    replace("[", "").
                    replace("]", "").
                    replace(", ", "+");
System.out.println(output);

output

输出

 https://www.google.com/#q=please+help+me

#2


3  

Though there is already an accepted answer, I am giving some alternatives:

尽管已经有了一个公认的答案,但我还是给出了一些替代方案:

  1. Java 8 String join

    Java 8字符串连接

    If you are using Java 8, it already provides a join method that you can make use of:

    如果您正在使用Java 8,它已经提供了一个连接方法,您可以使用:

    return "https://www.google.com/#q=" + String.join("+", args);
    

    (If you are using Java < 8, there are still lots of similar util you can find, like Commons Lang)

    (如果您正在使用Java < 8,仍然可以找到许多类似的util,如Commons Lang)

  2. Join with Loop

    加入循环

    It is also not difficult to write a proper and concise loop for this:

    也不难为这写一个适当而简洁的循环:

    StringBuilder result = new StringBuilder("https://www.google.com/#q=");
    boolean first=true;
    for (String arg : args) {
        result.append(first? "" : "+").append(arg);
        first = false;
    }
    return result;
    

    Yet other form, as someone in comment seems does not like a boolean flag:

    然而,另一种形式,正如某些人在评论中所说,似乎不喜欢布尔标志:

    StringBuilder result = new StringBuilder();
    for (String arg : args) {
        result.append(result.length() == 0 ? "https://www.google.com/#q=" : "+")
              .append(arg);
    }
    return result;
    

#3


0  

You can use the below method :

你可以使用以下方法:

public static String join(String[] array, String separator) {
    if (array == null) {
        return null;
    } else {
        if (separator == null) {
            separator = "";
        }

        if (array.length <= 0) {
            return "";
        } else {
            StringBuilder buf = new StringBuilder(array.length * 16);

            for (int i = 0; i < array.length; ++i) {
                if (i > 0) {
                    buf.append(separator);
                }

                if (array[i] != null) {
                    buf.append(array[i]);
                }
            }

            return buf.toString();
        }
    }
}

It's actually imported from org.apache.commons.lang3; package

它实际上是从org. apache.commonslang3导入的;包

Example :

例子:

public static String onGoogleCommand(String[] args) {
    if (args.length == 0) {
        return "Type in a question after the google command!";
    }
    if (args.length >= 1) {
        return "https://www.google.com/#q=" + join(args, "+");
    }
    return "What?";
}

#4


0  

You could iterate over the args[] array and build your query string:

可以遍历args[]数组并构建查询字符串:

public String onGoogleCommand(String[] args) {
    if(args == null || args.length == 0) {
        return "Type in a question after the google command!";
    }
    StringBuilder queryString = new StringBuilder("https://www.google.com/#q=");
    queryString.append(args[0]);
    for (int i=1; i < args.length; ++i) {
        queryString.append("+").append(args[i]);
    }

    return queryString.toString();
}

#5


-1  

You just need to use a foreach loop, something like this can help:

你只需要使用foreach循环,类似这样的东西可以帮助:

if(args.length >= 1){
    String finalStr="";
    for(String currentStr:args){
        finalStr+=currentStr+"+";
    }
    finalStr= finalStr.substring(0, finalStr.length()-1);
}

Using this code you will have your search in finalStr, just append it's value to your URL, as you can see the symbol "+" is added after each element and I always remove the last element ("+") because it's unnecessary at the end of the String.

使用此代码,您将在finalStr中进行搜索,只需将其值附加到URL,因为您可以看到在每个元素之后都添加了符号“+”,而且我总是删除最后一个元素(“+”),因为在字符串末尾不需要它。

#1


3  

By using Arrays.toString and replace you can achieve the result you want

通过使用数组。toString和replace可以实现您想要的结果

String array[] = {"please", "help", "me"};
String output = "https://www.google.com/#q=" + Arrays.toString(array).
                    replace("[", "").
                    replace("]", "").
                    replace(", ", "+");
System.out.println(output);

output

输出

 https://www.google.com/#q=please+help+me

#2


3  

Though there is already an accepted answer, I am giving some alternatives:

尽管已经有了一个公认的答案,但我还是给出了一些替代方案:

  1. Java 8 String join

    Java 8字符串连接

    If you are using Java 8, it already provides a join method that you can make use of:

    如果您正在使用Java 8,它已经提供了一个连接方法,您可以使用:

    return "https://www.google.com/#q=" + String.join("+", args);
    

    (If you are using Java < 8, there are still lots of similar util you can find, like Commons Lang)

    (如果您正在使用Java < 8,仍然可以找到许多类似的util,如Commons Lang)

  2. Join with Loop

    加入循环

    It is also not difficult to write a proper and concise loop for this:

    也不难为这写一个适当而简洁的循环:

    StringBuilder result = new StringBuilder("https://www.google.com/#q=");
    boolean first=true;
    for (String arg : args) {
        result.append(first? "" : "+").append(arg);
        first = false;
    }
    return result;
    

    Yet other form, as someone in comment seems does not like a boolean flag:

    然而,另一种形式,正如某些人在评论中所说,似乎不喜欢布尔标志:

    StringBuilder result = new StringBuilder();
    for (String arg : args) {
        result.append(result.length() == 0 ? "https://www.google.com/#q=" : "+")
              .append(arg);
    }
    return result;
    

#3


0  

You can use the below method :

你可以使用以下方法:

public static String join(String[] array, String separator) {
    if (array == null) {
        return null;
    } else {
        if (separator == null) {
            separator = "";
        }

        if (array.length <= 0) {
            return "";
        } else {
            StringBuilder buf = new StringBuilder(array.length * 16);

            for (int i = 0; i < array.length; ++i) {
                if (i > 0) {
                    buf.append(separator);
                }

                if (array[i] != null) {
                    buf.append(array[i]);
                }
            }

            return buf.toString();
        }
    }
}

It's actually imported from org.apache.commons.lang3; package

它实际上是从org. apache.commonslang3导入的;包

Example :

例子:

public static String onGoogleCommand(String[] args) {
    if (args.length == 0) {
        return "Type in a question after the google command!";
    }
    if (args.length >= 1) {
        return "https://www.google.com/#q=" + join(args, "+");
    }
    return "What?";
}

#4


0  

You could iterate over the args[] array and build your query string:

可以遍历args[]数组并构建查询字符串:

public String onGoogleCommand(String[] args) {
    if(args == null || args.length == 0) {
        return "Type in a question after the google command!";
    }
    StringBuilder queryString = new StringBuilder("https://www.google.com/#q=");
    queryString.append(args[0]);
    for (int i=1; i < args.length; ++i) {
        queryString.append("+").append(args[i]);
    }

    return queryString.toString();
}

#5


-1  

You just need to use a foreach loop, something like this can help:

你只需要使用foreach循环,类似这样的东西可以帮助:

if(args.length >= 1){
    String finalStr="";
    for(String currentStr:args){
        finalStr+=currentStr+"+";
    }
    finalStr= finalStr.substring(0, finalStr.length()-1);
}

Using this code you will have your search in finalStr, just append it's value to your URL, as you can see the symbol "+" is added after each element and I always remove the last element ("+") because it's unnecessary at the end of the String.

使用此代码,您将在finalStr中进行搜索,只需将其值附加到URL,因为您可以看到在每个元素之后都添加了符号“+”,而且我总是删除最后一个元素(“+”),因为在字符串末尾不需要它。