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
"thi i ome text"
char
array from the string using string.toCharArray()
method. char
array. Append the char
to a String if it is not equal to your target char
StringBuilder
class for appending: building the string. You can iterate through the String using charAt()
String s = "this is some text";
public static String remove(String a, char b) {
String word = "";
for (int i = 0; i < a.length(); i++){
if (s.charAt(i) != b){
word += a.charAt(i);
}
}
return word;
}
An alternative to String concatenating is to use StringBuilder
inside the loop
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.length(); i++){
if (s.charAt(i) != b){
sb.append(s.charAt(i));
}
}
return sb.toString();
Another way to approach this is to convert the String to a char array and traverse the array
StringBuilder sb = new StringBuilder;
for (char c : s.toCharArray()){
if (c != b) {
sb.append(c);
}
}
return word;
I'll not suggest an alternative solution , I'll tell you how to fix the problem in your current code.
Your program works only for the first occurrence of s
. You should do the following:
" "
. remove
method. " "
. Should have something like that:
String sampleString = "this is some text";
String[] arr = sampleString.split(" ");
char sampleChar = 's';
for(String str : arr) {
System.out.print(remove(str, sampleChar) + " ");
}
Note that in remove
method, you should check if the String contains s
, if not, return it as it is:
public static String remove(String a, char b) {
if(!a.contains(b)) {
return a;
}
String newString = "";
newString = a.substring(0, a.indexOf(b))+""+a.substring(a.indexOf(b)+1);
return newString;
}
This should work.
You can use recursion like below. But still use StringBuilder,that s the better way. This is just to make your code work
public static String remove(String a, char b)
{
String newString = "";
newString = a.substring(0, a.indexOf(b))+""+a.substring(a.indexOf(b)+1);
if(newString.indexOf(b)!=-1) {
newString= remove(newString,b);
}
return newString;
}
You can convert the source string to char array using a.toCharArray())
,
then write a for-loop to compare the char element in char array one-by-one with expected removed char.
If it is not equal, then append it to StringBuilder object.
have a try with this;
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;*/
StringBuilder sb = new StringBuilder();
for(char c: a.toCharArray())
{
if(c!= b)
{
sb.append(c);
}
}
return sb.toString();
}
public class charReplace{
public static void main(String args[]){
String sampleString = "This is some text";
char sampleChar = 's';
System.out.println(char_remove(sampleString, sampleChar));
}
public static String char_remove(String a, char b){
String newString = " ";
int len = a.length();
for(int i =0; i<len ;i++)
{
char c = a.charAt(i);
if(c == b)
newString = newString + " ";
else
newString = newString + c ;
}
return newString;
}
}
public static String remove(String a, char b)
{
StringBuilder sbul =new StringBuilder();
int i;
for(i=0;i<a.length();i++){
char c = a.charAt(i);
if(c==b)
continue;
sbul.append(c);
}
return sbul.toString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.