Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Sorry for the basic question but I have a Java String that I need to return as part of a method as a Set<String>
I cannot find a reference on how to do this. Any clues?
The usual way to do this, with a normal set:
String s = "hello";
Set<String> set = new HashSet<>();
set.add(s)
return set;
Or if you prefer an immutable set with just a single element:
String s = "hello";
return Collections.singleton(s);
If you are using Java 9 or newer, use Set.of():
return Set.of("foo");
Collections.singleton("yourString")
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string that contains String q = "What's the value of √32 and √83?";, my problem is replacing √num with sqrt(32) and sqrt(83).
That means my string should output : -
What's the value of sqrt(32) and sqrt(83)
is it possible?
Use a regex replacement on the pattern √(\d+)\b, and replace with sqrt(...):
String q = "What's the value of √32 and √83?";
String output = q.replaceAll("√(\\d+)\\b", "sqrt($1)");
System.out.println(output);
This prints:
What's the value of sqrt(32) and sqrt(83)?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to make a method return that return several times a String from a list of String. any idea ??
How about something like this:
String getRandomString(List<String> list) {
return list.get(new Random().nextInt(list.size()));
}
This isn't the most efficient way, but should do the job.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What I have:
String result = "<>hello<!><>Soumik<!><>Having a wonderful day?<!>";
What I need:
resultStrings = ["hello", "Soumik", "Having a wonderful day?"];
This regex should do the trick:
<[^>]*>([^<]+)<
Find all matches, and extract capturing group 1 from each.
Regex demo
How about that:
result = result.replace("<", "");
result = result.replace(">","";
resultStrings = result.split("!");
It's really simple.
I don't know other conditions so it may not be useful. Please add conditions so I can respond to it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got many resources with names based on a pattern:
R.drawable.small_house_red
R.drawable.small_house_blue
R.drawable.big_house_black
R.drawable.small_tree_red
I've got 3 strings: size, type, color. I can put all required resources to map, and get them by concated string but is it possible to get them without manually entering all combinations to the map?
You can use this
int id = context.getResources().getIdentifier("imageNameAsString", "drawable", context.getPackageName());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how to sort variables X and negated varibles X' alphabetic which are saved as string in Java?
Example: String string = "B*A'*D*H'"; result must
String sortedString="A'*B*D*H'";
are the variables always separated with * ?
In that case it'd be very simple:
String in = "B*A'*D*H'";
String[] vars = in.split("\\*");
Arrays.sort(vars);
// Java is missing a simple php-like join function ;-(
StringBuilder sorted = new StringBuilder();
for(String s: vars){
sorted.append(s);
sorted.append("*");
}
sorted.deleteCharAt(sorted.length()-1);
and you're done ;-)