我想使用索引值打印一個子字符串。我需要在計數時排除空白區域,但它應該輸出空白區域。我想要顯示來自主字符串的n個字母。空格將保持原樣,但從下限到上限索引的字母數應爲n。我的代碼是如何指定要包含在子字符串中的字母數(而不是空格)?
public class Test {
public static void main(String args[])
{
String Str=new String("Welcome to the class");
System.out.println("\nReturn value is:");
System.out.println(Str.substring(2,9));
}
}
輸出: lcome牛逼
在上述代碼,它計算之間的「歡迎」和「到」的空間。我不需要計算它們之間的空間。我的預期輸出是lcome到
作爲一個說明,你幾乎不應該使用'new String(String)'。只需使用'str =「Welcome」;'。 – chrylis
爲什麼你需要避免計算空間? – Ephi
你究竟想問什麼。請具體說明。謝謝。 –