Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting values from strings

 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic