Sorting Variables and negated Variables in Java [closed] - java

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 ;-)

Related

Search Substring(specific value) from array index in java [closed]

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 last month.
Improve this question
I want to filter specific text from a specific array in java like
String a="code=500,code1=1000"
I want to get only 500 from index 0 by searching "code="
One approach would be to:
Split the String by "," to an array.
Now for each element of the array, do a replaceAll to remove the text before the number.
It might not be the most optimal, but this should work:
String[] split = a.split(",");
for (String s: split) {
String desiredText = s.replaceAll(".+=", "");
}
Then you could add the desiredText to a List or whatever it is you want to do with it.

ascii value as string to character in java [closed]

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 1 year ago.
Improve this question
I have a String value "\u0004"
I need to convert it to character '\u0004'
I need to store this character back to the string.
How can this be done?
Maybe this is answer you want
// string value that has "\" in front
String s = "\\u0004";
// substring to leave "\u" out
char c = (char)Integer.parseInt(s.substring(2));

Replace all √num from a string java [closed]

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)?

Converting a String to Set<String> [closed]

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")

extract an array from list in java [closed]

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 have a list such:
List<string> list =Arrays.asList("2014-06-20 [txt1:executed]","2014-06-21 [txt2:in progressed]");
I want to extract the contents to an array:
arr[0] =2014-06-20
arr[1] =executed
arr[2] =2014-06-21
arr[3] =in progressed
You might take a look at regular expressions. They are quite powerfull to split Strings according to your wishes.
Here is an example how you can split your String.
String regex = "([0-9-]+)\\s+\\[\\w*:([\\w\\s]*)]";
List<String> list = Arrays.asList("2014-06-20 [txt1:executed]","2014-06-21 [txt2:in progressed]");
String date = list.get(0).replaceAll(regex, "$1");
String status = list.get(0).replaceAll(regex, "$2");
I hope that helps.

Categories