Write a method which will remove any given character from a string?
Answers
Answered by
0
I need to write a method called remove() that takes two arguments, a String and a char, and returns a new String that removes all occurrences of the char from the input String. I have to do this without using the replace() method. So far I have:
♠. public class Remover { public static void main (String[] args) { String sampleString = "this is some text"; char sampleChar = 's'; System.out.print(remove(sampleString, sampleChar)); } public static String remove(String a, char b) { String newString = ""; newString = a.substring(0, a.indexOf(b))+""+a.substring(a.indexOf(b)+1); return newString; } }
The main method is just there for testing purposes and the sample string and char are just examples; I need this method to work for any string and char. But either way, I'm sure what I have is wrong and I'm completely lost on how to proceed. For this case, I'm trying to get my code to return.
In middle the program is written.. from ♠ it's starts
I hope it can help you.....
♠. public class Remover { public static void main (String[] args) { String sampleString = "this is some text"; char sampleChar = 's'; System.out.print(remove(sampleString, sampleChar)); } public static String remove(String a, char b) { String newString = ""; newString = a.substring(0, a.indexOf(b))+""+a.substring(a.indexOf(b)+1); return newString; } }
The main method is just there for testing purposes and the sample string and char are just examples; I need this method to work for any string and char. But either way, I'm sure what I have is wrong and I'm completely lost on how to proceed. For this case, I'm trying to get my code to return.
In middle the program is written.. from ♠ it's starts
I hope it can help you.....
Similar questions
History,
8 months ago
English,
8 months ago
Environmental Sciences,
1 year ago
Social Sciences,
1 year ago
Physics,
1 year ago
Science,
1 year ago