• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching a String for a sequence of characters  RSS feed

 
Tracy Pierce
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I search a string for a sequence of adjacent characters? For example, I need to find the full sequence "<c>" within the string.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the Javaranch.

If you just want the index of "<c>" then you should look at the String methods. If you need something more complex then you can look at regex.
 
Tracy Pierce
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the prompt response, and welcome!

For what I'm trying to do I need to analyze a given string, for the text between the tags "<c>" and "</c>", and replace what's already in the string with the comment between those two tags.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Pattern and Matcher if you want to search Strings for patterns / regex's

something like ..



would give you the index of each match in your string, or you can just use...



if you just need to find one occurance of <c>.

 
Tracy Pierce
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure of the actual text given, but I need to be able to search for multiple occurrences of these tags.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you should look at the regex tutorial. But before you do that: the text you're looking for looks like xml. In case it is you should probably use an xml parser. For simple stuff regex can work fine but you'll run into serious problems once your xml gets more complex.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernie Mcracken wrote:or you can just use...



if you just need to find one occurance of <c>.

You can also use indexOf to find multiple occurrences:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!