extract an array from list 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 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.

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.

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

How do I find all substrings within two common characters in a given 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 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.

Trying to pull out certain numbers from a string [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 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));

Sorting Variables and negated Variables 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 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 ;-)

Categories