Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace characters in a string in Java without using .replace?

Tags:

java

string

The goal of this program is to prompt the user for a single character and a phrase, and then replace any instances of that character within that phrase with a '$'. My program below does just that, but when I showed it to my professor I was told that I cannot use .replace in the methods I built, so I have to figure out a way to not use that. I have worked at it for a while, and thus far I know that I can replace it with a for loop, but after several frustrating iterations, I can't seem to get it right. Excuse me if my code looks funky, I am still an introductory java student so I'm still learning the basics. I have provided a proposed solution at the end of my code snippet below.

public static char getKeyCharacter(String userInput) {

    char keyCharacter;
    Scanner inputStream = new Scanner(System.in);
    while(userInput.length() > 1) 
    {
        System.out.println("Please enter a SINGLE character to use as key: ");
        userInput = inputStream.nextLine();
    }
    keyCharacter = userInput.charAt(0);

    return keyCharacter;
}
public static String getString(String userResponse) {

    Scanner inputStream = new Scanner(System.in);
    String theString;
    while(userResponse.length() > 500) {
        System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
        userResponse = inputStream.nextLine();
    }
    while(userResponse.length() < 4) {
        System.out.println("Please enter a phrase or sentence >= 4 and <=500 characters: ");
        userResponse = inputStream.nextLine();

    }

    theString = userResponse;
    return theString;

}
public static String maskCharacter(String theString, char keyCharacter){

    String maskedString = "";
    final char mask = '$';
    maskedString = maskedString + theString.replace(keyCharacter, mask);
    System.out.println("String with " + keyCharacter + " masked: ");
    return maskedString;
}

public static String removeCharacter(String theString, char keyCharacter) {

    String modifiedString = " ";
    final char replaceChar = ' ';
    modifiedString = modifiedString + theString.replace(keyCharacter, replaceChar);
    System.out.println("String with " + keyCharacter + " removed:");
    return modifiedString;

}

public static int countKey(String theString, char keyCharacter) {
    int charCount = 0;
    for (int c = 0; c < theString.length(); c++) {
        if (theString.charAt(c) == keyCharacter) {
            charCount++;
        }
    }
    System.out.println("Occurences of " + keyCharacter + " in string:");
    return charCount;
}  

}

I believe the solution is will look something like this, but thus far I've been unsuccesful -

public static String maskCharacter(String theString, char keyCharacter){

    String maskedString = "";
    final char mask = '$';
    for (int k = 0; k < theString.length(); k++) {
        if (theString.charAt(k) == keyCharacter) {
            keyCharacter = mask;
         }
    System.out.println("String with " + keyCharacter + " masked: ");
    return maskedString;
}

My issue lies in making the maskedString = theString with all the keyCharacters replaced by mask. For the record, I have yet to learn anything about those fancy arrays, so if there is a way to do this using a simple for loop I would greatly appreciate it. Thank you for the assistance in advance!

like image 294
bobdylan7 Avatar asked May 20 '26 04:05

bobdylan7


2 Answers

I would use a StringBuilder and String#toCharArray() with a simple for-each loop. Like,

public static String maskCharacter(String theString, char keyCharacter){
    StringBuilder sb = new StringBuilder();
    for (char ch : theString.toCharArray()) {
        if (ch == keyCharacter) {
            sb.append('$'); // <-- mask keyCharacter(s).
        } else {
            sb.append(ch); // <-- it isn't the character to mask
        }
    }
    return sb.toString();
}
like image 189
Elliott Frisch Avatar answered May 21 '26 16:05

Elliott Frisch


I wouldn't use a StringBuilder: just use the result of toCharArray() directly:

char[] cs = theString.toCharArray();
for (int i = 0; i < cs.length; ++i) {
  if (cs[i] == keyCharacter) cs[i] = '$';
}
return new String(cs);

Not only is it more concise, but:

  • It will run faster, because it's cheaper to access an array element than to invoke a method; and because it doesn't require StringBuilder's internal buffer to resize (although you could just pre-size that);
  • It will use less memory, because it doesn't require storage for the copy inside StringBuilder.
like image 35
Andy Turner Avatar answered May 21 '26 18:05

Andy Turner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!