string - Remove punctuation java -
i'm trying remove punctuation string keep spaces, need able distinguish different words. end goal find length of each word in string.
i set for
loop check length of word until hits space count punctuation letter. know have change variable in if statement reflect length of substring between i
, indexof
space in string.
for(int i=0; > stringlength - 1;){ original.substring(i, original.indexof(' ')); if(i > minlength)
while might tempting throw bunch of fors , ifs, cleaner use regular expression:
pattern.compile("[.,; ]+").splitasstream(input)
a full example:
import java.util.regex.pattern; import java.util.stream.collectors; public class counting { public static void main(string... args) { string text = "this string. punctuation, care words."; string wordswithlengths = pattern.compile("[.,; ]+") .splitasstream(text) .map(word -> word + " => " + word.length()) .collect(collectors.joining("\n")); system.out.println(wordswithlengths); } }
output:
this => 4 => 2 => 1 string => 6 => 4 => 4 punctuation => 11 => 3 => 1 => 4 care => 4 => 5 words => 5
also, if want count how many words have more n characters can:
import java.util.regex.pattern; public class countingwords { public static void main(string... args) { string text = "this string. punctuation, care words."; int threshold = 5; long amountofwords = pattern.compile("[.,; ]+") .splitasstream(text) .filter( word -> word.length() > threshold) .count(); system.out.println("there " + amountofwords + " words more " + threshold + " characters"); } }
Comments
Post a Comment