Given a sentence with multiple spaces between words and also spaces in front and back of sentence how do you remove these extra spaces
-- during interview, try to give solution that use linux command or regex. It may be not what interviewer want, but show that u know something. and can solve things in different ways.
https://github.com/mission-peace/interview/blob/master/src/com/interview/regex/MultiSpaceReplacement.java
public void replace(String str){
Pattern pattern = Pattern.compile("^ +| +| +$");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.replaceAll(""));
}
public static void main(String args[]){
String str = " This is Tushar Roy ";
MultiSpaceReplacement mrs = new MultiSpaceReplacement();
mrs.replace(str);
}
-- during interview, try to give solution that use linux command or regex. It may be not what interviewer want, but show that u know something. and can solve things in different ways.
https://github.com/mission-peace/interview/blob/master/src/com/interview/regex/MultiSpaceReplacement.java
public void replace(String str){
Pattern pattern = Pattern.compile("^ +| +| +$");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.replaceAll(""));
}
public static void main(String args[]){
String str = " This is Tushar Roy ";
MultiSpaceReplacement mrs = new MultiSpaceReplacement();
mrs.replace(str);
}