Simple string question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Simple string question

  1. #1
    Don LLoyd Guest

    Simple string question


    Haven't looked at Java in a couple of years but now I've got fix a problem
    in somebody else's code and I'm hitting a wall. All I've really got to do
    is take one string and strip out all occurences of "{}" (i.e. not strip out
    all brackets, but all left-right pairs with no space in between).

    I found documentation for String.replaceAll, but it doesn't seem to actually
    exist ("Cannot resolve symbol" error at compile).

    There's got to be a simple way to do this that I'm overlooking - any suggestions?



  2. #2
    Kunal Guest

    Re: Simple string question


    Try this method:

    replace(char oldChar, char newChar)

    Returns a new string resulting from replacing all occurrences of oldChar
    in this string with newChar.



    "Don LLoyd" <don@drl2.com> wrote:
    >
    >Haven't looked at Java in a couple of years but now I've got fix a problem
    >in somebody else's code and I'm hitting a wall. All I've really got to

    do
    >is take one string and strip out all occurences of "{}" (i.e. not strip

    out
    >all brackets, but all left-right pairs with no space in between).
    >
    >I found documentation for String.replaceAll, but it doesn't seem to actually
    >exist ("Cannot resolve symbol" error at compile).
    >
    >There's got to be a simple way to do this that I'm overlooking - any suggestions?
    >
    >



  3. #3
    Moppy Guest

    Re: Simple string question


    Dont know if there is a way provided already by java to do what u asked but
    to create the method itself was quite quick and easy.

    I think this works.

    public String replaceAll(String from, String token)
    {
    int index = 0;
    int lastindex = 0;
    String rtrnStr = new String();

    while(lastindex < from.length() &&
    (index = from.indexOf(token, lastindex)) != -1)
    {
    rtrnStr += from.substring(lastindex, index);
    lastindex = index+token.length();
    }
    rtrnStr += from.substring(lastindex, from.length());

    return rtrnStr;
    }

    Hope was of help


    "Don LLoyd" <don@drl2.com> wrote:
    >
    >Haven't looked at Java in a couple of years but now I've got fix a problem
    >in somebody else's code and I'm hitting a wall. All I've really got to

    do
    >is take one string and strip out all occurences of "{}" (i.e. not strip

    out
    >all brackets, but all left-right pairs with no space in between).
    >
    >I found documentation for String.replaceAll, but it doesn't seem to actually
    >exist ("Cannot resolve symbol" error at compile).
    >
    >There's got to be a simple way to do this that I'm overlooking - any suggestions?
    >
    >



  4. #4
    Don LLoyd Guest

    Re: Simple string question


    That did it! Thanks.


    "Moppy" <djavro@start.com.au> wrote:
    >
    >Dont know if there is a way provided already by java to do what u asked

    but
    >to create the method itself was quite quick and easy.
    >
    >I think this works.
    >
    >public String replaceAll(String from, String token)
    >{
    > int index = 0;
    > int lastindex = 0;
    > String rtrnStr = new String();
    >
    > while(lastindex < from.length() &&
    > (index = from.indexOf(token, lastindex)) != -1)
    > {
    > rtrnStr += from.substring(lastindex, index);
    > lastindex = index+token.length();
    > }
    > rtrnStr += from.substring(lastindex, from.length());
    >
    > return rtrnStr;
    >}
    >
    >Hope was of help
    >
    >
    >"Don LLoyd" <don@drl2.com> wrote:
    >>
    >>Haven't looked at Java in a couple of years but now I've got fix a problem
    >>in somebody else's code and I'm hitting a wall. All I've really got to

    >do
    >>is take one string and strip out all occurences of "{}" (i.e. not strip

    >out
    >>all brackets, but all left-right pairs with no space in between).
    >>
    >>I found documentation for String.replaceAll, but it doesn't seem to actually
    >>exist ("Cannot resolve symbol" error at compile).
    >>
    >>There's got to be a simple way to do this that I'm overlooking - any suggestions?
    >>
    >>

    >



  5. #5
    Bryan Dollery Guest

    Re: Simple string question


    There is a considerably easier way to do this, if you know regular expressions.
    Start by importing the jakarta ORO libraries (you can find these on the jakarta
    site

    http://jakarta.apache.org/oro/index.html

    Once the library (.jar) is in your classpath you need to import the relavant
    bit:

    import org.apache.oro.text.perl.Perl5Util;

    Now you can use the Perl5Util class to do regular expression matches and
    substitutions. Here we'll use the substitution method. I've written a little
    method to demonstrate how to do that:

    private void test() {
    Perl5Util util = new Perl5Util();
    String test = "This is a {}test";
    test = util.substitute("s/{}//gmx", test);
    if(test.equals("This is a test"))
    System.out.println("Passed");
    else
    System.out.println("Failes");
    }

    The important line is the one that calls the substitute method. It contains
    a regular expression that searches for the string "{}", and replaces it with
    nothing. The 'gmx' bit tells it that it should do this as many times as it
    finds a match in the string, that it should consider the string to contain
    newline characters, and that this is an extended regular expression. However,
    you needn't worry about any of that. You only need ot replace the string
    'test' with your string, and remove all the other lines (except the one declaring
    the util object. So, you get your substitution in two lines.

    Java 1.4 has something quite similar built in, but it's unlikely that you'll
    have the benefit of that, so these ORO utilities are the easiest thing you
    can do here. It has the benefit of being terribly fast too.

    Hope this helps

    Bryan


    "Don LLoyd" <don@drl2.com> wrote:
    >
    >That did it! Thanks.
    >
    >
    >"Moppy" <djavro@start.com.au> wrote:
    >>
    >>Dont know if there is a way provided already by java to do what u asked

    >but
    >>to create the method itself was quite quick and easy.
    >>
    >>I think this works.
    >>
    >>public String replaceAll(String from, String token)
    >>{
    >> int index = 0;
    >> int lastindex = 0;
    >> String rtrnStr = new String();
    >>
    >> while(lastindex < from.length() &&
    >> (index = from.indexOf(token, lastindex)) != -1)
    >> {
    >> rtrnStr += from.substring(lastindex, index);
    >> lastindex = index+token.length();
    >> }
    >> rtrnStr += from.substring(lastindex, from.length());
    >>
    >> return rtrnStr;
    >>}
    >>
    >>Hope was of help
    >>
    >>
    >>"Don LLoyd" <don@drl2.com> wrote:
    >>>
    >>>Haven't looked at Java in a couple of years but now I've got fix a problem
    >>>in somebody else's code and I'm hitting a wall. All I've really got to

    >>do
    >>>is take one string and strip out all occurences of "{}" (i.e. not strip

    >>out
    >>>all brackets, but all left-right pairs with no space in between).
    >>>
    >>>I found documentation for String.replaceAll, but it doesn't seem to actually
    >>>exist ("Cannot resolve symbol" error at compile).
    >>>
    >>>There's got to be a simple way to do this that I'm overlooking - any suggestions?
    >>>
    >>>

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center