txt.replaceAll("a","b");
Is "a" a Char Sequence or a Regex (or more specific Literal Search)?
And is my code correct? I’m coding the Exercise "Normalize Text". Task:
- Only one space between words.
- Only one space after comma (,), dot (.) and colon (:). First character of word after dot is in Uppercase and other words are in lower case.
Please correct me if I am wrong, including my English.
public class NormalizeText {
static String spacesBetweenWords(String txt){
txt = txt.replaceAll(" +", " ");
return txt;
}
/**
* - There are no spaces between comma or dot and word in front of it.
* - Only one space after comma (,), dot (.) and colon (:).
*/
static String spacesCommaDotColon(String txt) {
txt = txt.replaceAll(" +\\.", ".");
txt = txt.replaceAll(" +,", ",");
txt = txt.replaceAll(" +[:]", ":");
txt = txt.replaceAll("[.]( *)", ". ");
txt = txt.replaceAll("[,]( *)", ", ");
txt = txt.replaceAll("[:]( *)", ": ");
//txt.replaceAll("a","b");
return txt;
}
public static void main(String[] args) {
// TODO code application logic here
String txt = "\" \\\" i want to f\\\"ly\" . B.ut : I , Cant\\";
System.out.println(txt);
txt = spacesBetweenWords(txt);
System.out.println(spacesBetweenWords(txt));
System.out.println(spacesCommaDotColon(txt));
}
}
My teacher said my code is not using regex, but rather a Char Sequence. I am very confused.
txt.replace("a","b");instead ofreplaceAll,replaceAllis always regex. Note thatreplacestill replaces all occurrences.