Reputation: 70
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
?
Upvotes: 2
Views: 111
Reputation: 40056
Though there is already an accepted answer, I am giving some alternatives:
Java 8 String join
If you are using Java 8, it already provides a join method that you can make use of:
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)
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;
Upvotes: 4
Reputation: 522032
You could iterate over the args[]
array and build your query string:
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();
}
Upvotes: 0
Reputation: 9
You just need to use a foreach loop, something like this can help:
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.
Upvotes: -1
Reputation: 12840
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
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?";
}
Upvotes: 0
Reputation: 44854
By using Arrays.toString
and replace
you can achieve the result you want
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
Upvotes: 3