• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String parsing

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I need a extract a substring from a given string

For example,

"\"LT-1-1-1:EVLT-F,EVLT-F:SWOVERRULE=NOOVERRULE,LSMPWR=UP,EQPTPROFNAME=VC8DEFAULT,,RSTCAUSE=POWERON,RSTTIME=70-01-01,00:03:45,RSTNUM=0,OAMIPADDR=0-0-0-0,PAIRNUMBER=0,DUALHOSTIPADDR=0-0-0-0,DUALHOSTLSMLOC=0-0:IS-NR,\""

Note the entire string comes in one single line and i need to take out the substring

SWOVERRULE=NOOVERRULE,LSMPWR=UP,EQPTPROFNAME=VC8DEFAULT,,RSTCAUSE=POWERON,RSTTIME=70-01-01,00:03:45,RSTNUM=0,OAMIPADDR=0-0-0-0,PAIRNUMBER=0,DUALHOSTIPADDR=0-0-0-0,DUALHOSTLSMLOC=0-0




Can anyone please help.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the delimiters always stay the same you could simply work with substring and indexOf ...
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears you need the string between the two colons. The methods Sebastian suggested can help you out.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several ways to do it, depending on what exactly you need to get.

As Sebastian said, if you know you always need characters x through y, you'd go about it one way.

if you know you need everything after "F:" and everything before ":IS-NR", you'd do something else.

It really all depends on HOW you can determine what you need. define the problem better, and you'll get better suggestions here.
 
Raja Mirrah
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need everything between those two colons. but the problem here is may get a colon in between but i should take the string from second colon from starting till the last colon in the string.

Hope the above explanation is clear.

Note i do not know what string is coming all i have to do is extract from second colon from starting till last colon at the end of string.

Thanks all your quick response.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raja Mirrah wrote:I need everything between those two colons. but the problem here is may get a colon in between but i should take the string from second colon from starting till the last colon in the string.

Hope the above explanation is clear.



As already mentioned, with the indexOf() and / or lastIndexOf() methods, you should be able to find the two colons, and using substring(), you should be able to extract the string that you need.

Henry
 
Raja Mirrah
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

How to take the indexOf for second colon, i aware that the last colon can be taken using lastindexof method.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raja Mirrah wrote:Henry,

How to take the indexOf for second colon, i aware that the last colon can be taken using lastindexof method.




Although not elegant, the easiest way is to call the indexOf() method twice -- the second time using an starting index that is after the index found in the first call.

Henry
 
Destroy anything that stands in your way. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic