• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Searching a String for a sequence of characters

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic