I should write a function that excludes certain letters (char) of a sentence (String). I basically also did that but the problem is I only managed to cut out the 1st appearance of the letter in the sentence.
public static void main (String[] args) {
String text = "This text may be readable without vowels!";
String letters = "aeiou";
Out.println(removeLetters(text, letters));
}
public static String removeLetters(String text, String letters) {
char c = 'f';
String remover = text;
for (int i=0; i<letters.length(); i ) {
c = letters.charAt(i);
remover = removeChar(remover, c);
}
return remover;
}
public static String removeChar(String text, char c) {
int i1 = text.indexOf(c);
String result = text.substring(0, i1) text.substring(i1 1);
return result;
}
How do I need to change the last function removeChar
to get all appearances of a letter cut out? It might not be that hard to find all indexes but the real struggle is to put the substrings together afterwards, so that you still only have the one sentence left just without the certain letters. Because the more indexes you have the more different substrings you need to add together if I understood it correctly.
Currently I get this.
Ths txt my be readable witht vowels!
And the aim would be to get here:
Ths txt my b rdbl witht vwls!
CodePudding user response:
And here is one way that uses a regular expression. It simply replaces each vowel in the character class [aeiou]
with an empty string.
String text = "This text may be readable without vowels!";
text = text.replaceAll("[aeiou]","");
System.out.println(text);
prints
Ths txt my b rdbl wtht vwls!
CodePudding user response:
My suggestion is to use a loop inside the removeChar
function.
Add a while
loop which breaks if text.indexOf(c)
returns -1 (which means that the string doesn't contain the letter you are searching for anymore).
This way you will find all occurence of each letter you want to remove from the source string.
Example code for the removeChar function:
public static String removeChar(String text, char c) {
String result = text;
int i1 = text.indexOf(c);
while (i1 != -1) {
System.out.println("Index of " c " in " result " is: " i1);
String part1 = result.substring(0, i1);
System.out.println(part1);
String part2 = result.substring(i1 1);
System.out.println(part2);
result = part1 part2;
i1 = result.indexOf(c);
}
return result;
}
CodePudding user response:
"Classical" variant with loop:
public static String removeLetters(String text, String strLetters) {
Set<Character> letters = new HashSet<>();
for (char i : strLetters.toCharArray()) {
letters.add(i);
}
StringBuilder sb = new StringBuilder();
for (char i : text.toCharArray()) {
if (!letters.contains(i)) sb.append(i);
}
return sb.toString();
}
CodePudding user response:
Here are two approaches using streams
:
Option 1:
You can stream
the text using chars()
, and then .filter()
the letters:
public static String removeLetters(String text, String letters) {
return text.chars().filter(c -> letters.indexOf(c) == -1)
.mapToObj(Character::toString).collect(Collectors.joining());
}
Option2:
You can stream
the letters using chars()
, and then use .reduce()
to remove the chars
from text
:
public static String removeLetters(String text, String letters) {
return letters.chars().mapToObj(Character::toString)
.reduce(text, (str, c) -> str.replaceAll(c, ""));
}
Then:
String text = "This text may be readable without vowels!";
String letters = "aeiou";
System.out.println(removeLetters(text, letters));
Output:
Ths txt my b rdbl wtht vwls!