• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with 'subString and replaceAll'

 
Aparna Ram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Any guidance?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aparna Ram:
Any guidance?


Please read this.
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hey thanks Tony.. it works
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions are also a perfect solution for a task like this:
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic