Remove sub string from a string in JAVA.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teenIce
    New Member
    • Aug 2007
    • 36

    Remove sub string from a string in JAVA.

    Hi all,

    Does anyone have suggestion what can I do to remove some string from a string?
    Like this :

    Original : I have a cat.
    Remove : have
    Result : I a cat.

    Thanks in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by teenIce
    Hi all,

    Does anyone have suggestion what can I do to remove some string from a string?
    Like this :

    Original : I have a cat.
    Remove : have
    Result : I a cat.

    Thanks in advance.
    The specs for the String class has the method you're looking for.

    Comment

    • praveen2gupta
      New Member
      • May 2007
      • 200

      #3
      Originally posted by teenIce
      Hi all,

      Does anyone have suggestion what can I do to remove some string from a string?
      Like this :

      Original : I have a cat.
      Remove : have
      Result : I a cat.

      Thanks in advance.
      Hi
      There is a String class and some methods are associated with it. Read this class and their methods , your problem will be solved

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by praveen2gupta
        There is a String class and some methods are associated with it. Read this class and their methods , your problem will be solved
        The String class should solve the problem, providing you don't need anything exotic (like "delete all words that start with an 'h'"). But even then, you'll be able to use the given methods to solve the problem, it will be a bit more work though.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by nepomuk
          The String class should solve the problem, providing you don't need anything exotic (like "delete all words that start with an 'h'"). But even then, you'll be able to use the given methods to solve the problem, it will be a bit more work though.
          Regular expressions

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by r035198x
            Regular expressions
            Arg, didn't think of that. But at least I wasn't totally mistaken:
            The package java.util.regex is only provided from java 1.4, some of it even from java 1.5 onwards, so for backwards compatibility.. . ^^ OK, hardly anyone uses something older than 1.5 these days, but you never know! :-D

            Comment

            • gaya3
              New Member
              • Aug 2007
              • 184

              #7
              Originally posted by teenIce
              Hi all,

              Does anyone have suggestion what can I do to remove some string from a string?
              Like this :

              Original : I have a cat.
              Remove : have
              Result : I a cat.

              Thanks in advance.

              Hi,
              following code will help u..

              class Stringremoval
              {
              public static void main(String args[])
              {
              String sample="i have book";
              StringBuffer s = new StringBuffer(sa mple);
              StringBuffer AfterRemoval=s. delete(2,6);
              System.out.prin tln("removed part:"+removedp art);
              }
              }


              -Thanks & Regards
              Hamsa

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by gaya3
                Hi,
                following code will help u..

                class Stringremoval
                {
                public static void main(String args[])
                {
                String sample="i have book";
                StringBuffer s = new StringBuffer(sa mple);
                StringBuffer AfterRemoval=s. delete(2,6);
                System.out.prin tln("removed part:"+removedp art);
                }
                }


                -Thanks & Regards
                Hamsa
                Spoiler: removing part of a String is identical to replacing that part of the String
                by nothing, i.e. the empty String "". There's no need for all those statements
                (what is "removedpar t"?)

                kind regards,

                Jos

                Comment

                • teenIce
                  New Member
                  • Aug 2007
                  • 36

                  #9
                  Hi,

                  I had found the code to replace sub string in string. The code can be like this :

                  Code:
                  String tmpString = myString.replace(textToReplace,"");
                  Thanks all.. :)

                  Comment

                  • gaya3
                    New Member
                    • Aug 2007
                    • 184

                    #10
                    Originally posted by JosAH
                    Spoiler: removing part of a String is identical to replacing that part of the String
                    by nothing, i.e. the empty String "". There's no need for all those statements
                    (what is "removedpar t"?)

                    kind regards,

                    Jos


                    hi,
                    sry its not "removedpar t" in tat pgm... its "Afterremov al" which is string...
                    wat u said also rite..(removing --replacing the string with"")

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by teenIce
                      Hi,

                      I had found the code to replace sub string in string. The code can be like this :

                      Code:
                      String tmpString = myString.replace(textToReplace,"");
                      Thanks all.. :)
                      Great work. You'll often find that those doc pages usually have what you need.

                      Comment

                      Working...