2016-07-06 17 views
4

我想使用索引值打印一個子字符串。我需要在計數時排除空白區域,但它應該輸出空白區域。我想要顯示來自主字符串的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到

+3

作爲一個說明,你幾乎不應該使用'new String(String)'。只需使用'str =「Welcome」;'。 – chrylis

+1

爲什麼你需要避免計算空間? – Ephi

+1

你究竟想問什麼。請具體說明。謝謝。 –

回答

6

你可以用簡單的數學。只需對它進行子串處理,即可刪除所有空白,並將原始長度與沒有空格的String進行比較。之後,將大小差異添加到子字符串的結束索引中。

public static void main(String args[]) { 
    String Str = "Welcome to the class"; 
    System.out.println("\nReturn value is:"); 
    String sub = Str.substring(2, 9); 
    String wsRemoved = sub.replaceAll(" ", ""); 
    String wsBegginingRemoved = sub.replaceAll("^ *", ""); 
    String outputSub = Str.substring(2+(sub.length()-wsBegginingRemoved.length()), 9+(sub.length()-wsRemoved.length()+(sub.length() - wsBegginingRemoved.length()))); 
    System.out.println(outputSub); 
} 

編輯:不能忽視領先空格了

O/P

lcome to 

O/P "My name is Earl"

name is E  
+1

這不能正常工作!你可以在位置9之後留出一些空位,你會省略它們。反例:「我的名字是伯爵」返回「名字是」而不是「名字是E」。 – majk

+0

正確,這不起作用。 – fuzz

+0

@KevinEsche:據我所知,但如果我得到OP所要求的,這種情況下的預期輸出是「名稱是E」,以包含7個非空白字符。 – majk

-2

您可以使用replaceFirst此:

String Str = "Welcome to the class"; // remove new String() 
Str = Str.replaceFirst("^ *", ""); 
System.out.println("\nReturn value is:"); 
System.out.println(Str.substring(2, 10)); // increment end index by 1 

輸出:

lcome to

4

一種方法是將其解壓縮到使用正則表達式^.{2}([^ ] *){7}

另一種選擇是使用簡單的for循環遍歷字符串並計算用於子字符串的終點。

int non_whitespace = 0; int i; 
for(i = 2; non_whitespace < 7; ++non_whitespace, ++i) { 
    while (str.charAt(i) == ' ') ++i; 
} 
return str.substring(2, i); 

您認爲哪種方法更具可讀性,並評估哪一種方法在速度問題上會導致更好的性能。

0

如果你不想使用正則表達式,你可以實現你自己的版本substring。在簡單的解決方案:

private static String substring(int begin, int end, String str) { 
    StringBuilder res = new StringBuilder(); 
    while (begin < end) { 
     if (str.charAt(begin) == ' ') { 
      end++; 
     } 
     res.append(str.charAt(begin)); 
     begin++; 
    } 
    return res.toString(); 
} 

這裏的技巧是忽略空間的「計數」,通過增加end遇到的真實時,強制循環,使一個額外的迭代。

代碼複雜度是O(n)。


System.out.println(substring(2, 9, "Welcome to the class")); 
>> lcome to 
+1

從性能角度來看,最好不要使用StringBuilder,而是使用一個子字符串調用的計算索引來計算返回值。 – majk

+1

@majk我不明白。 – Maroun

+0

是否有一個downvote的原因? – Maroun

3

你想要做的是顯示ñ從字符串包括空格,但ň不包括沒有的字符數。的空白。爲此,您可以簡單地使用循環而不是庫函數。

邏輯:在while循環中繼續顯示從index = 2到index = 9-1的字符串str的字符。如果當前字符是空格,則將子字符串的字符串索引的上限值n的值增加1,即程序現在將顯示超出每個空白的上限的額外字符遇到的空間。

考慮下面的代碼。

String str = "Welcome to the class"; 
int index = 2, n = 9; 
while(index < n){ 
    char c = str.charAt(index); 
    System.out.print(c); 
    if(c==' ') 
    n++; 
    index++; 
} 

輸出:lcome to
希望您能理解這些代碼。


編輯
作爲@Finbarr O'B說,一檢查,以防止StringIndexOutOfBoundsException會的計劃,其時,循環必須被定義爲是必要的:

while(index < n && index < str.length()){ 
    ... 
} 
+3

好的答案,我只會添加一個字符串邊界檢查,以確保我們不' t嘗試引用超出字符串的長度 –

+1

@ FinbarrO'Brien爲此,我們只需將while循環定義爲'while(index progyammer

+0

是的,這是一種可能性。當索引超出字符串末尾時,它將完成。 –

相關問題