Trying to pull out certain numbers from a string [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
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));

Related

how do i compare each character of a same string using a for loop? [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 5 years ago.
Improve this question
So I have to make a program where I have to compare each character of a string to see if they occur in the string more than once or not, if they occur more than once then I have to delete that string from an array list for instance "peer" and "pear" then it should remove "peer" because there are 2 e's in "peer", so how do I go about doing this?
Here is a hint. To check whether there is any duplicate character you can use a Set.
For each character of String
check if the char is in the set
if yes
then you can delete this String from arraylist and stop checking this string
else
add this char to the set
Try this: if(Pattern.matches("\w{2,}",ID)==true){//do what you want}. Then I wish you know how to remove the string.
Pythonic answer:
charlist=['a','b','c','d','e','f','g','h','i','j','k','p','r']
stringlist=['peer','pear']
for string in stringlist:
for character in charlist:
if string.count(character) > 1:
stringlist.remove(string)
print (stringlist)

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.

What is Move the decimal value and correct value 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 6 years ago.
Improve this question
I have an amount of 12.35 like this:
Double x = 12.35
And I would like to change the amount to 35.12 like this:
println x >> 35.12
I am not able to find a solution in Java, where I could do that.
What you're trying to do isn't a mathematical operation, so you really shouldn't try to treat it as one. Instead, treat it as a string manipulation.
Convert the number to a String, and split it up at the decimal point. Then recombine the string with the before and after parts swapped.
String[] xParts = Double.toString(x).split("[.]");
System.out.println(xParts[1] + "." + xParts[0]);

trying to obtain a first name in the path after first back slash [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 want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.

Regular expression to find a middle of a String 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 9 years ago.
Improve this question
I have a string
1,11,cda74944-1c61-4325-8137-83a4ab50cb81-A00123,Bs000000216,20140204185143.811
Can you provide me regular expression or java string function to get the sub string:
cda74944-1c61-4325-8137-83a4ab50cb81-A00123 ?
You split the string:
String[] array = String.split(",");
This will return an Array of String like this
["1", "11", "cda74944-1c61-4325-8137-83a4ab50cb81-A00123", "Bs000000216", "20140204185143.811"]
You can access the part you want in array[2].
If there is a string like this, use pattern:
/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{6}/
The pattern is:
[a-f0-9]++(?>-[a-z0-9]++)+
You can split the String
String[] allStrings= String.split(",");
then try to get allStrings[2] //it will give you the desired result cda74944-1c61-4325-8137-83a4ab50cb81-A00123
A few options:
Using length:
String s = yourString.subString(0, yourString.length() >>> 1);
Using split:
String s = yourString.split(",")[2];
Alternatively, if you have something representing physical data that is always represented similarly, it might be smarter to wrap it in a class instead.

Categories