You can do it in two steps by chaining String#replaceAll
. In the first step, replace the regex, [a-z]
, with ?
. The regex, [a-z]
means a character from a
to z
.
public class Main {
public static void main(String[] args) {
String str = "I Like Java";
str = str.replaceAll("[a-z]", "?").replaceAll("\\s+", "+");
System.out.println(str);
}
}
Output:
I+L???+J???
Alternatively, you can use a StringBuilder
to build the desired string. Instead of using a StringBuilder
variable, you can use String
variable but I recommend you use StringBuilder
for such cases. The logic of building the desired string is simple:
Loop through all characters of the string and check if the character is a lowercase letter. If yes, append ?
to the StringBuilder
instance else if the character is whitespace, append +
to the StringBuilder
instance else append the character to the StringBuilder
instance as it is.
Demo:
public class Main {
public static void main(String[] args) {
String str = "I Like Java";
StringBuilder sb = new StringBuilder();
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (Character.isLowerCase(ch)) {
sb.append('?');
} else if (Character.isWhitespace(ch)) {
sb.append('+');
} else {
sb.append(ch);
}
}
// Assign the result to str
str = sb.toString();
// Display str
System.out.println(str);
}
}
Output:
I+L???+J???
If the requirement states:
- The first character of each word is a letter (uppercase or lowercase) which needs to be left as it is.
- Second character onwards can be any word character which needs to be replaced with
?
.
- All whitespace characters of the string need to be replaced with
+
.
you can do it as follows:
Like the earlier solution, chain String#replaceAll
for two steps. In the first step, replace the regex, (?<=\p{L})\w
, with ?
. The regex, (?<=\p{L})\w
means:
\w
specifies a word character.
(?<=\p{L})
specifies a positive lookbeghind for a letter i.e. \p{L}
.
In the second step, simply replace one or more whitespace characters i.e. \s+
with +
.
Demo:
public class Main {
public static void main(String[] args) {
String str = "I like Java";
str = str.replaceAll("(?<=\\p{L})\\w", "?").replaceAll("\\s+", "+");
System.out.println(str);
}
}
Output:
I+l???+J???
Alternatively, again like the earlier solution you can use a StringBuilder
to build the desired string. Loop through all characters of the string and check if the character is a letter. If yes, append it to the StringBuilder
instance and then loop through the remaining characters until all characters are exhausted or a space character is encountered. If a whitespace character is encountered, append +
to the StringBuilder
instance else append ?
to it.
Demo:
public class Main {
public static void main(String[] args) {
String str = "I like Java";
StringBuilder sb = new StringBuilder();
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i++);
if (Character.isLetter(ch)) {
sb.append(ch);
while (i < len && !Character.isWhitespace(ch = str.charAt(i))) {
sb.append('?');
i++;
}
if (Character.isWhitespace(ch)) {
sb.append('+');
}
}
}
// Assign the result to str
str = sb.toString();
// Display str
System.out.println(str);
}
}
Output:
I+l???+J???