Android Question [Solved] How to search for a specific text in a String

masterleous

Member
Licensed User
Longtime User
Hi all,

I need some help in finding some specific text from a string.

For Example;

String Text = “DataA:2345.9, DataB:1234.7, DataC:87659.2, DataD:987241………..DataM:2345.0”

DataA/B/C is the name of Data Value (and is for example, actual names are quite different to each another, but are same to other strings) and values are in between “:” and “,”.

First Step: I want to search about complete data, If String has all Data names and its values (In above example from DataA to DataM) than application should have to proceed to Second step otherwise set high or low one flag bit just to highlight that corresponding String don’t have complete data (Setting flag bit is not difficult for me). The problem is to find out “Data names” inside message in First step; I tried different search commands but failed to do so. The only way I found is to search for a character inside text string, which is a lengthy process to find each character individually as don’t know the correct index of character due to different values.

Second Step: I want to copy DataA, DataB, DataC and other Data values to save as a XML or Text file. This is difficult for me to split one string in different ways. As in one string there are so many Names and their values.

I need help in this and waiting for any response.

Thanks
 

udg

Expert
Licensed User
Longtime User
Seems a regex could help here.
May I suggest to have a look about regular expressions and then eventually come back to the thread with what you achieved so far?
See it just as a way to have the opportunity to get a closer look at a powerful tool, not a smart way to evade your question or, worse, negate any help.

udg
 
Upvote 0

udg

Expert
Licensed User
Longtime User
pattern field: (Data.) : ([^,]+)
Doesn't that need a final asterisk sign?
pattern field: (Data.) : ([^,]+)*

udg
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I tested it with:
DataA:2345.9,DataB:1234.7,DataC:87659.2,DataD:987241,DataM:2345.0
Without the added final asterisk it returns just last group couple (DataM and 2345.0)
Adding an asterisk it returns all the group couples
BTW, I had to add a space after the last significant data digit (0 in 2345.0) otherwise the on-line tool showed no results.

Forget it: I inadvertently added a space as trailing char for the pattern, so the above.
Your code works nice as suggested in post #3. Sorry for the false alarm... :oops:
 
Upvote 0
Top