Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing specific character from the given String  RSS feed

 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hot to remove a specific character from the given string , take an example of

String str="N''E'E'''R''A'''J";


Now I want to remove the single quotes from the string str , without using loops .
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String str="N''E'E'''R''A'''J";

You won't get one String from that. You need to use escape
characters and write " as \" and ' as \' such as in
String str = "N\'\'E\'E\'\''R\''A\''\'J" ;
(I can't be sure what you had because I can't see what was a double quote and what was 2 single quotes.)

Check out the String classes split methods.
[ September 17, 2004: Message edited by: Elouise Kivineva ]
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out the java API for String

there are several versions of the replace method.


pascal
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't nee to escape single quotes in a String. Assuming bold is double quotes, This is legal: "'".

You might want to specifically look at the replaceAll method of String.
[ September 17, 2004: Message edited by: Paul Sturrock ]
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Class1
{
public static void main(String[] args)
{
System.out.println("dd");
String str="r'o'h'a'n";
String str1=str.replace('\'','');
System.out.println(str1);
}
}

I used this code but this is not working
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

is illegal. A char is a primitive type so can't be null. And so you can't use it in the String replace(char oldChar, char newChar) method. I'd re-read the JavaDocs more carefully, and pay attention to Pascal's post

there are several versions of the replace method.

[ September 17, 2004: Message edited by: Paul Sturrock ]
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rohan,

In your question you have asked about removing a charater from the string. I see that in the above posts, everyone has talked about replacing it. You have to know that replacing is possible in strigs but if you want to delete/remove a character from a string, I dont think that can be done.Rememeber, Strings are Immutable. You might like to use the StringBuffer instead. To know about the options you have in the StringBuffer, try the following command:

javap java.lang.StringBuffer

( similarly to see the methods available in "String" class u can use :
javap java.lang.String )

And after doing this you can have a look at the method that gives you the option to delete a character at a certain position in a string.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:


But I think it should be:
 
Hosh Nasi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a tokenizer to sepperate by the ' then concatinate the strings together?
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rovas Kram:
I think it should be:


That method doesn't exist. Since 1.4 you should be able to write

str.replaceAll("'", "");

though.
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prashant bhogvan:
In your question you have asked about removing a charater from the string. I see that in the above posts, everyone has talked about replacing it. You have to know that replacing is possible in strigs but if you want to delete/remove a character from a string, I dont think that can be done.Rememeber, Strings are Immutable.


You're right that Strings are immutable. But that actually means that the original object can't be changed at all - both replacing and removing would create a new String object.

You might like to use the StringBuffer instead.


No need to use StringBuffer in this case.

To know about the options you have in the StringBuffer, try the following command:

javap java.lang.StringBuffer


JavaDoc might actually be more convenient:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll
 
Mahesh Bhatt
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right there . Can u please tell me, if it's possible to see the javadoc without being online. I usually use "javap" as a quick reference, however javadoc is a much better option . It gives me everything in detail.
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can download a local copy of the Javadoc from java.sun.com.
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But I think it should be:
[/qb]<hr></blockquote>
[ September 20, 2004: Message edited by: Rohan Kayan ]
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prashant bhogvan:
you are right there . Can u please tell me, if it's possible to see the javadoc without being online. I usually use "javap" as a quick reference, however javadoc is a much better option . It gives me everything in detail.


You might like to download a fully searchable version from

here
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!