(String).trim [1]
public String trim()

文字列の前後の空白を除去します。

String s1 = "   Java and Python    ";
System.out.println(s1.trim());