By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

Remove sub string from a string in JAVA.

P: 37
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.
Aug 23 '07 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
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.
Aug 23 '07 #2

100+
P: 201
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
Aug 23 '07 #3

Nepomuk
Expert 2.5K+
P: 3,112
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.
Aug 23 '07 #4

10K+
P: 13,264
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
Aug 23 '07 #5

Nepomuk
Expert 2.5K+
P: 3,112
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
Aug 23 '07 #6

100+
P: 184
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(sample);
StringBuffer AfterRemoval=s.delete(2,6);
System.out.println("removed part:"+removedpart);
}
}


-Thanks & Regards
Hamsa
Aug 23 '07 #7

Expert 10K+
P: 11,448
Hi,
following code will help u..

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


-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 "removedpart"?)

kind regards,

Jos
Aug 23 '07 #8

P: 37
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. String tmpString = myString.replace(textToReplace,"");  
Thanks all.. :)
Aug 24 '07 #9

100+
P: 184
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 "removedpart"?)

kind regards,

Jos


hi,
sry its not "removedpart" in tat pgm... its "Afterremoval" which is string...
wat u said also rite..(removing --replacing the string with"")
Aug 24 '07 #10

10K+
P: 13,264
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. String tmpString = myString.replace(textToReplace,"");  
Thanks all.. :)
Great work. You'll often find that those doc pages usually have what you need.
Aug 24 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.