• 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

Help with 'subString and replaceAll'

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

I have a String variable holding value


I would like to display only HATCHER MARY from the above value.
How do I use subString and replaceAll methods to do the same?

I am only able to display the whole string over and again.
Kindly help since I am all new to the world of programming.

Thanks.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please show us what you've tried by posting more code. From just this one line I can't even tell if you're using Java or JSP.
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do use JSF here.. but rephrasing..

How do I extract the String "HATCHER MARY" from the String
"<SPAN>HATCHER MARY</SPAN>"

Knowing this would make things easier for me.

Thanks.
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any guidance?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aparna Ram:
Any guidance?



Please read this.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to decide on what part of this string is fixed and what part is variable eg are you trying to extract the text from between SPAN tags or is it extract the text from between the first '>' and the next '<' characters.

For the later you could use something like:

 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But this happens to be urgent, thats why the impatience.
Sorry forum!
 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks Tony.. it works
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular expressions are also a perfect solution for a task like this:
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
Regular expressions are also a perfect solution for a task like this:

I agree that using a regex can easily solve this problem. However, I believe the best approach to use depends on the complexity of the problem and the context in which it is being used. There is also a significant performance overhead associated with using a regex which may or may not be an issue.
For simple searches, such as the one I demonstrated I would favour a simple approach whereas for more complex searches (ie ones that can't be handled by a few lines of simpe code) I'd definitely go with a regex approach.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aparna Ram:

But this happens to be urgent, thats why the impatience.
Sorry forum!



It is only urgent for you.

If it is important to you to get a quick reply, the best you can do is try to make your question as well formed as possible. Adding urgent to the post or subject line or bumping the post to the top of the thread without adding any more meaningful information usually tends to slow down responses or halt them altogether.

To learn why, see How To Ask Questions On JavaRanch
 
reply
    Bookmark Topic Watch Topic
  • New Topic