Thursday, November 15, 2007

[JAVA] Remove leading or trailing spaces in a String using regular expressions

import java.util.regex.*;

public class BlankRemover
{

/* remove leading whitespace */
public static String ltrim(String source) {
return source.replaceAll("^\\s+&qu ot;, "");

}

/* remove trailing whitespace */
public static String rtrim(String source) {
return source.replaceAll("\\s+$&qu ot;, "");
}

/* replace multiple whitespaces between words with single blank */

public static String itrim(String source) {
return source.replaceAll("\\b\\s{2 ,}\\b", " ");
}

/* remove all superfluous whitespaces in source string */
public static String trim(String source) {

return itrim(ltrim(rtrim(source) ));
}

public static String lrtrim(String source){
return ltrim(rtrim(source));
}

public static void main(String[] args){
String oldStr =

"> <1-2-1-2-1-2-1-2-1-2-1> <";
String newStr = oldStr.replaceAll("-", " ");
System.out.println(newStr);

System.out.println(ltrim(newStr));
System.out.println(rtrim(newStr));
System.out.println(itrim(newStr));
System.out.println(lrtrim(newStr));
}
}

Centos 7 reset root/ any user lost password / lockout due to cant remember password

1. Need to be in front of the terminal. (Physically if not vm). 2. Reboot the server 3. Press 'e' in the GRUB2 boot screen. 3. bunch...