Results 1 to 3 of 3

Thread: DOM tree

  1. #1
    shadow240 is offline Member
    Join Date
    Nov 2014
    Rep Power

    Question DOM tree

    How do i write a method in java that will add a <b> or <em> tag to a specific word regardless of case or punctuation
    for example for "run forest RUN!" adding bold to run would be <b>run<b> & <b>RUN!<b>

    public void addTag(String word, String tag) {

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: DOM tree

    The String class has a method to ignore case when comparing Strings.
    Ignoring punctuation would require the code to know what characters are punctuation so it could strip them when accessing the words in the String. Perhaps any character that is not a-z could be considered punctuation.
    Last edited by Norm; 03-23-2015 at 09:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: DOM tree

    ... or use regex. The punctuation group is defined in the Pattern class. As is a case-insensitive flag, and capturing groups.
    Java Code:
    String addTag(String text, String word, String tag) {
        return text.replaceAll("(?i)\\Q" + word + "\\E\\p{Punct}*", "<" + tag + ">$0</" + tag + ">");
    Use that without understanding it at your own risk.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Display rich:tree in expandable mode when page loads with tree
    By rajpan in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 08-29-2012, 02:33 PM
  2. Replies: 2
    Last Post: 07-24-2012, 11:52 PM
  3. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 02:08 AM
  4. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  5. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts