How to get last few characters of varying length from a string ..? - java

I have a multiple string urls, from which i have to pick last few characters, which are id's infact. But the problem is that, the length of id's is not consistent, i.e., if one id is of length 6 then, other may be of length 5 or 4 and so on. The sample urls are like:
www.abc.com/xyz-123456
www.abc.com/pqr-5432
www.abc.com/lmn/opqr-25647
it could have been a lot easier if the length of the particular id portion would have been same, i could have used:
String abc = "www.abc.com/xyz-123456";
String id = abc.substring(abc.length()-6);
But now the scenario is different as length of id portion in the selected url is not the same always, How can i cater this varying id..???? please any help is appreciated.

There is a lastIndexOf method on the String object that will let you find the position of the '-' (I take it that is your separator). From there you can do the substring.

You can use something like this.
String id=abc.subString(abc.lastIndexOf('\'),abc.length()-1);
Hope it will help you. :)

String url1 = "www.abc.com/xyz-123456";
String[] url1Split = url1.split("-");
What you're looking for can be found in url1split[1]

Use regex to remove all characters upto -.
String id = url.replaceAll("^.*-","");
or
String id = url.replaceAll("^.*-(\\w+)$","$1");

You can use LastIndexOf or create the regular expression.

Related

Java remove dynamic substring from string

I need to remove dynamic substring from string. There is a few similar topic of this theme, but noone of them helped me. I have a string e.g.:
product test1="001" test2="abc" test3="123xzy"
and i need output:
product test1="001" test3="123xzy"
I mean I need remove test2="abc". test2 is an unique element and can be placed anywhere in original string. "abc" is dynamic variable and can have various length. What is the fastest and the most elegant solution of this problem? Thx
You can use a regular expression:
String input = "product test1=\"001\" test2=\"abc\" test3=\"123xzy\"";
String result = input.replaceAll("test2=\".*?\"\\s+", "");
In substance: find a substring like test2="xxxxxx", optionally followed by some spaces (\\s+) and replace it with nothing.

Regex to extract value from link

I have a String like file:///android_asset/GwyXUyisyq. I want to extract the GwyXUyisyq from the rest of the string. The value will change in every instance, but the file:///android_asset/ will always remain fixed. What regex can I use to achieve the same?
You don't need a regex here :
Just find the last index of / and replace everything before it :)
String s = "file:///android_asset/GwyXUyisyq";
System.out.println(s.replace(s.substring(0,s.lastIndexOf("/")+1), ""));
O/P :GwyXUyisyq

Regular expression to retreieve integer

Hi I have an URL like "/yyyyyy/xm", where x can be an integer denoting the number of minutes. I need to parse and get this value. Any idea of how this can be using regex or String.split() method? The pattern of the URL is always the same like for example:
/magnetic/20m should give me the value 20
/temperature/500m should give me the value 500
Thanks in advance!
The following should work:
/.*?/(\d+)
You just need to access to the 1st group of the match, and you'll get the numbers there.
Edit:
In the future, finding the regex by yourself. That's a pretty straightforward regex question.
And if you don't like regexp...
String txt = "/magnetic/20m";
String[] components = txt.split("/");
String lastComponent = components[components.length - 1];
int result = Integer.parseInt(lastComponent.replace("m", ""));

regular expression Help on - extract a value from a string ignoring the previous contents

can someone help me to write a java regex to retrieve a value from the following string please?
XX0001 15NOV XXX SELECTED RAX AXXXXX DXXXXXXXXX REF NBR 002 SSSS
I wanted to extract the value 002. All the strings / characters before 002 are fixed length and properly padded with trailing space (if req.). could have any string/numeric/special displayable characters.
I am looking for something like ... get 002 from that position ignoring whatever before. ?
What language?
If javascript:
myCode=myString.substring(56,58);
or
myCode=myString.substr(56,3);
If PHP:
$myCode=substr($myString,56,3);
This simpler option is preferable to regex because it is faster. You can use this because you're working with fixed length strings.
EDIT: Just saw your edit referencing Java. So in Java:
String myCode = myString.substring(56,59);
In Java
string.substring(56,59);
You don't need regex to do that. Just use the String method substring:
String myString = originalString.substring(106,109); // myString = "002"
106 is the begin index, and 109 the end index - 1. To simply get the first, just take the length of the original string just before the number you want to get, for instance:
System.out.println("XX0001 15NOV XXX SELECTED RAX AXXXXX DXXXXXXXXX REF NBR ".length());
Assuming that you want the last set of digits before the end of the string, you might want to do something like this:
^.*\\d{3}\\s+.{4}$
This should instruct the Regex Engine to start matching from the beginning of the string, match any characters and then, from the end, match 3 numbers, a space and any 4 characters.
Also, if you have fixed sizes and lengths, you can most likely get away with a .substring method, it is less complex.

How do I split a concatenated string into multiple floating point values?

I'm a begginer in java I have
packet=090209153038020734.0090209153039020734.0
like this I want to split this string and store into an array like two strings:
1) 090209153038020734.0
2) 090209153039020734.0
I have done like this:
String packetArray[] = packets.split(packets,Constants.SF);
Where:
Constants.SF=0x01.
But it won't work.
Please help me.
I'd think twice about using split since those are obviously fixed width fields.
I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:
STX (0x01).
0x0f.
date (YYMMDD or DDMMYY).
time (HHMMSS).
0x02.
value (XXXXXX.X).
0x03.
0x04.
And, given that they're fixed width, you should probably just use substrings to get the information out.
The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
You have your String packet;
String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write
int position = packet.indexOf(".")+1
+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.
Then we use substring
String first = packet.substring(0,position) will give you everything up to the ".0"
String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.
Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.
int position = packet.indexOf(".")+1
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;
String packetArray[] = packets.split("\u0001");
should work. You are using
public String[] split(String regex, int limit)
which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).
You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.
As Aaron Digulla mentioned, use the other version.
You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.
If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};
int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};
Your solution confuses the regexp with the string.
split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".
I think you are trying to use it like the .net String.Split(...) function?

Categories