Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How do I remove a character from a String?

java string char removing char

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 dreddooo

dreddooo

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 01 October 2015 - 02:29 PM

How do we remove a character from a string then concat them to form a new word?

Ex.

  1. User inputs the word "skin"
  2. Program is made to remove character 1 from input
  3. Program reads "skin" then prints out "sin"

I am just not sure what built-in java method to put in my code. Or does Java have one?

 

Here is how my code looks like right now:

import java.io.*;

public class Nuke2{
	public static void main(String[]args) throws Exception{
		BufferedReader keyboard;
		String oldStr, newStr;

		System.out.println("What's the word?");
		keyboard = new BufferedReader (new InputStreamReader(System.in));
		oldStr = keyboard.readLine();
		newStr = oldStr.removeCharAt(1); //possible new method?

		System.out.println(newStr);	
	}
}

is string.removeCharAt() a built-in java method?



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 02 October 2015 - 06:32 AM

You could use the replace method

 

newStr = oldStr.replace("X","");  // removes all X's from the string.  

 

 

The first parameter is a regex, so it's very powerful


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download