i have a string:
String value = "(name) Says: hello (/name)";
where “name” could also be “lastName”, “address” “postalCode” or many many other things.
i split the string on the “(”
i want to also split on the “)” … however, some strings will only contain the following:
String value = "(name Says: hello (/name)";
my problem is that the string content could also contain “)” and “(” so how would i parse this then? For instance:
String value = "(name Says: can you solve for y = (x-b)? (/name)";
the string could also be as follows:
String value = "(name)Says: can you solve for y = (x-b)? (/name)";
OR
String value = "(name) Says: can you solve for y = (x-b)?(/name)";
OR
String value = "(name)Says: can you solve for y = (x-b)?(/name)";
i was initially thinking that i could do a count of how many “)” there are in a string. However, if there are more than 2, i’m not sure what i should do? The reason why i would need to know how many “)” there are in a string is because i want to isolate “name”
any ideas? (i don’t want to use any libraries, just want to come up with the logic )…
The question wasn’t very clear to me. However,as far as I could understand,I guess that you want to capture the name and also the sentence that follows it,both seperately.Please correct me if I am wrong.
Here’s the solution:
1.Parse your String until the first space after “(” .
2.Store it in a temp String. Its length is given by
temp.length().3.So check if
temp.charAt(temp.length()-1)==')'.4.If yes then name would be in the substring
temp.substring(1,temp.length()-2).5.Otherwise it would be in the substring
temp.substring(1,temp.length()-1).6.Now the value after name can be stored in another
Stringuntil you find"(/".Hope it helps.