7 Replies - 4794 Views - Last Post: 31 August 2010 - 03:32 PM Rate Topic: -----

#1 kneil_20  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 27-August 09

Removing a Character From a String

Posted 31 August 2010 - 04:38 AM

how to remove a character in a string

lets say I have
import java.util.Scanner;

public class CharacterRemover{
public static void main(String[]args){
String word = "abcd+def";
//what do I do to remove e? or any of the variables or operand
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Removing a Character From a String

#2 kneil_20  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 27-August 09

Re: Removing a Character From a String

Posted 31 August 2010 - 04:39 AM

how to remove a character in a string

lets say I have
public class CharacterRemover{
public static void main(String[]args){
String word = "abcd+def";
//what do I do to remove e? or any of the variables or operand
}
}


to be specific
I want to combine all the elements in a Stack argh its hard to explain.
Stack s = new Stack();
Stack combine = new Stack();
s.push('a');
s.push('b');
//what will I do so the combine Stack will be updated like below
combine.push("ab")
s.pop();
//what will I do so the combine Stack will be updated like below
combine.push("a");



Elements in s : a
Elements in combine : ab , a

This post has been edited by kneil_20: 31 August 2010 - 05:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: Removing a Character From a String

Posted 31 August 2010 - 05:06 AM

Hi there,

Consider the following example:

String word = "abcd+def";  

public static String removeChar(String word, char e) {

   String r = "";

   for (int i = 0; i < word.length(); i ++) {
      if (word.charAt(i) != e) r += word.charAt(i);
   }

   return r;
}



So yeah, then "r" would be the string the method returns (excluding the character 'e')...

Hope this helps or atleast pushes you in the right direction!
Have fun :turned:

This post has been edited by Dean_Grobler: 31 August 2010 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Removing a Character From a String

Posted 31 August 2010 - 05:10 AM

Please use the [ code ] tag correctly ;)

Check this out.

You probably want to use the replace or substring methods...

This post has been edited by aniri: 31 August 2010 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Removing a Character From a String

Posted 31 August 2010 - 05:14 AM

Why you created 2 threads for same problem?
I guess this thread of yours also states the same problem.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Removing a Character From a String

Posted 31 August 2010 - 05:39 AM

Merged duplicate topics. Do NOT create duplicates.

Also edited topic title from "help with program"

This post has been edited by JackOfAllTrades: 31 August 2010 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Removing a Character From a String

Posted 31 August 2010 - 07:04 AM

Could he not use charAt()?
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Removing a Character From a String

Posted 31 August 2010 - 03:32 PM

Two ways I can think of doing are:

To remove ALL "e"'s
s = s.replaceAll("e", "");



To remove that specific "e", I'd use a StringBuilder, since String concatenation is very time consuming...
StringBuilder sb = new StringBuilder(s);
sb = sb.deleteCharAt(place where e is);
s = sb.toString();



For a small simple replacement like yours, mine went way over, but for larger strings with more complexity, using raw Strings can be time consuming...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1