"String".trim [1]

文字列の前後の空白を除去します。ただし、全角スペースは除去されません。

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