how do you remove letters in a string?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
how do you remove letters in a string?
Hello!
I'm new here, just sign up today! Hoping to make new friends!
Create a new string that doesn't contain the character in question
public static String removeChar(String str, int position) { return str.subString(0,position)+str.subString(position,str.length()); }
this might help you a little bitpublic class StringFormat3 { public static void main(String[] args) { String name; name = "zabdiel"; //dont bother this part.. its only for measuring the lenght of a strings ,using .length() method. System.out.println("Your name consists of " + name.length() + " characters"); //the following statements shows how to extract a character or a String in a String. System.out.println(name.substring(0,1)); System.out.println(name.substring(1,2)); System.out.println(name.substring(2,3)); System.out.println(name.substring(3,4)); System.out.println(name.substring(4,5)); System.out.println(name.substring(5,6)); System.out.println(name.substring(6,7)); } }
another example..
this one will show you how to extract or remove a String.
this one differs to my first example..
public class Sample1 { public static void main(String[] args) { String name = "Zabdiel"; String extracted1, extracted2, extracted3; extracted1 = name.substring(1); extracted2 = name.substring(2); extracted3 = name.substring(3); System.out.println(extracted1); System.out.println(extracted2); System.out.println(extracted3); } }
How about using a StringBuilder?
StringBuilder (Java Platform SE 6), int)
// Json
ei.. in string builder? extract a string? can you please give a little example of that using stringbuilder sir?
tnx in advance!!
How about this then.
final StringBuilder stringBuilder = new StringBuilder("Please remove me"); System.out.println(stringBuilder.toString()); stringBuilder.delete(7, 14); System.out.println(stringBuilder.toString());
Output:
// JsonPlease remove me
Please me
ahh .delete() method.. whaa didnt notice it
hmmm ill keep that one
ahh theres a difference in .substring() method and .delete() method..
in substring() method it extracts the location of the specified character or string.
while in delete() method it deletes or removes the excess character or string behind the corresponding character that the users wants to display.
do i make sense sir?
hehe
StringBuilder.delete(intial value of the character that wil be displayed , the succeeding values that will be deleted);
StringBuilder stringBuilder = new StringBuilder("zabdiel"); stringBuilder.delete(1,7);
output:
z
Last edited by chronoz13; October 3rd, 2009 at 04:39 AM.
delete is only available on the StringBuilder though I believe.
// Json
use BufferString class is a good idea.
how sir? can you show a little example?
for example:if you want remove letters "ge" from string "gehaiming",you could do like the follow code.
StringBuffer sb=new StringBuffer("gehaiming"); sb=sb.replace(0,2,"");