Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Extracting values from strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting values from strings" Watch "Extracting values from strings" New topic
Author

Extracting values from strings

avishek bose
Greenhorn

Joined: Oct 12, 2011
Posts: 15
Hi

Can somebody help me in ectracting the value from the below strings

String[] arr = {DKMJ43_DNPTRTrang_900, DKHOT MANSTR_toi ra 2 , HDLIIR MONYRE_TCATRE rack2 f1 r5, GJHI43 TC 2, KIOLTRE8 IIUHTV32_treuw2 tre 3, DVTREQ ra 7, FOLITRW TC 5 }

From the above values I want to extract the values upto ra, tre, TC or rack
Ex = DKMJ43_DNPTRTrang_900 = DKMJ43_DNPTRTrang_900
DKHOT MANSTR_toi ra 2 = DKHOT MANSTR_toi
HDLIIR MONYRE_TCATRE rack2 f1 r5 = HDLIIR MONYRE_TCATRE
KIOLTRE8 IIUHTV32_treuw2 tre 3 = KIOLTRE8 IIUHTV32_treuw2


likewise


Thank
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1045
    
  10

You can use String.replaceFirst() to replace everything after and including the key word with the empty string. The danger is that the key word could be contained in the item to be extracted so care should be taken with the regular expression to make sure it is delimited front and back by a space.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

The first step is always to write down in English (or whatever natural language you choose) what you want to do. While examples are great and can help illustrate what you want, they don't DEFINE what you want.

Do that first and post it here. Then we can help you refine it until it is ready to be coded.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
avishek bose
Greenhorn

Joined: Oct 12, 2011
Posts: 15
Thanks richard for your help .. with regex i am able to resolve my query

fred , definitely i will try to define the problem along with example in my future issues..
 
Don't get me started about those stupid light bulbs.
 
subject: Extracting values from strings