Reputation: 8135
For examples, I have:
String s = "{\"ABC\"}:Mycontent{\"123\"}";
I only want to get the Mycontent{"123"} part and this part can be changed dynamically. Can anybody please show me how to do it (Fast performance)?
Upvotes: 2
Views: 23639
Reputation: 4381
Split method inside string can split a string around matches of regular expression so you can use this method. You can also use subsequence method if the position of the string you want is always the same. Also you can use the substring method. Many ways. Personal favorite is substring
str2=str.substring(begin, [end])
I put [] around end because it is not necessary. If you do not put an end index the substring will continue until the end of the string. Both indexes must be integers. Also this method returns a string.
If the size of the number changes dynamically aswell (e.g. 123 turns to 4567) you can solve this by not using an end index then using a find function and finding the character that comes after the last digit in your string (I suppose this does not change) and then replacing that character (using a replace method) with a \0 so the string ends there.
All the methods I have mentioned and there explanations can be found in javadoc
Upvotes: 4
Reputation: 24124
Using indexOf on the string, you can find the occurrence of ':' and get the substring from there as follows:
s.substring(s.indexOf(':') + 1)
If this is a structured output from an upstream system, you should look for standard way to interpret it (at first look, it appeared like JSON which means you should use a JSON parser. But it seems to be something else, you would know better about the source of this input).
Upvotes: 2