[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help with Java Strings

Posted on 2011-10-06
4
Medium Priority
?
227 Views
Last Modified: 2012-05-12
Dear fellow Java developers:

I am having problems with a java problem.  I have a method:

public String removeString(String firstString, String secondString)

The purpose of this method is to remove the characters in "firstString", and remove it from "secondString".  For example, if "firstString" is "aeiou", and "secondString" is "Hello World", the resulting string should be "Hll Wrld".  

How would I do this?

Thanks in advance to all who reply.
0
Comment
Question by:fsyed
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36927978
Somethidn like that:


StringBuffer sb = new StringBuffer();
for(int j=0,j<secondString.length();j++){
if(firstString.indeOf(secondString.charAt(j)+"") > -1)sb.add(secondString.charAt(j);


}

Systerm.out.println(sb.toString())

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36927981
rahter opposite:

StringBuffer sb = new StringBuffer();
for(int j=0,j<secondString.length();j++){
if(firstString.indeOf(secondString.charAt(j)+"") == -1)sb.add(secondString.charAt(j);


}

Systerm.out.println(sb.toString())

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36928005

This is how it work.
Let me know if you don't understand anything I'll explain

public class GetNewString{

    public static String getString(String firstString, String secondString){

    StringBuffer sb = new StringBuffer();
for(int j=0;j<secondString.length();j++){
if(firstString.indexOf(secondString.charAt(j)+"") == -1)sb.append(secondString.charAt(j));


}
  return sb.toString();

}

  public static void main(String[] args) {

        
               System.out.println(getString ("aeiou", "Hello World"));
}
}

Open in new window


Ouptput:

Hll Wrld

Open in new window

0
 

Author Closing Comment

by:fsyed
ID: 36928091
Thanks very much for your help.  Your solution worked!  Points well deserved.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question