2010-10-11 59 views
2

我剛剛寫了這個方法,我想知道如果類似的東西已經存在於框架中?它只是看起來像這些方法之一...獲得領先的空白

如果不是,有沒有更好的方法來做到這一點?

/// <summary> 
/// Return the whitespace at the start of a line. 
/// </summary> 
/// <param name="trimToLowerTab">Round the number of spaces down to the nearest multiple of 4.</param> 
public string GetLeadingWhitespace(string line, bool trimToLowerTab = true) 
{ 
    int whitespace = 0; 
    foreach (char ch in line) 
    { 
     if (ch != ' ') break; 
     ++whitespace; 
    } 

    if (trimToLowerTab) 
     whitespace -= whitespace % 4; 

    return "".PadLeft(whitespace); 
} 

感謝

編輯: 讀了一些評論,它清楚,我還需要處理的標籤後。

,因爲網站空間修剪下降到只有一個,但我會努力,我不能給出一個很好的例子:

說出輸入與5位的字符串,該方法將返回與4串空間。如果輸入少於4個空格,則返回""。 這可能有所幫助:

input spaces | output spaces 
0 | 0 
1 | 0 
2 | 0 
3 | 0 
4 | 4 
5 | 4 
6 | 4 
7 | 4 
8 | 8 
9 | 8 
... 
+0

你可以給一些樣本輸入/輸出嗎?目前還不完全清楚你想從你的代碼中做什麼。例如,如果第一個字符不是空白和'trimToLowerTab == false',則'whitespace == 0'。因此,無論線的長度如何,總是以「return」「.PadLeft(0)」結尾。如果第二個字符不是空格,則總是以1個空格結束,等等。我沒有看到這些情況下舍入的位置。多一點上下文也會有幫助。 – 2010-10-11 15:59:54

+0

所以如果我給字符串''e「'(想象3個空格),那麼方法返回的字符串應該是」「,因爲只有3個空格。但是,如果輸入字符串是「e」(5個空格),則返回的字符串將是'「」'(4個空格)(在空間總數之下的最接近4的倍數)。如果參數是錯誤的,那麼前導空格只是在沒有任何修改的情況下給出。編輯:網站從評論中刪除空白... – Nobody 2010-10-11 16:03:17

+0

你能編輯你的問題,並把你的例子作爲代碼嗎?我在您的評論中看不到超過一個連續的空間。 – 2010-10-11 16:05:48

回答

0

對String的擴展方法怎麼樣?我通過tabLength使函數更加靈活。我還添加了一個單獨的方法來返回空白長度,因爲一個評論是你正在尋找的。

public static string GetLeadingWhitespace(this string s, int tabLength = 4, bool trimToLowerTab = true) 
{ 
    return new string(' ', s.GetLeadingWhitespaceLength()); 
} 

public static int GetLeadingWhitespaceLength(this string s, int tabLength = 4, bool trimToLowerTab = true) 
{ 
    if (s.Length < tabLength) return 0; 

    int whiteSpaceCount = 0; 

    while (Char.IsWhiteSpace(s[whiteSpaceCount])) whiteSpaceCount++; 

    if (whiteSpaceCount < tabLength) return 0; 

    if (trimToLowerTab) 
    { 
    whiteSpaceCount -= whiteSpaceCount % tabLength; 
    } 

    return whiteSpaceCount; 
} 
+0

非常好,謝謝。我無法相信,我沒有想到它是一種擴展方法! – Nobody 2010-10-11 18:07:10

+0

如果您在具有5個空格的字符串上調用GetLeadingWhitespaceLength(),該代碼是否會引發異常? – 2014-01-31 16:14:56

+0

我現在沒有時間調試它,但我不認爲它會拋出。如果您通過SSSSSX(其中S ==空格),那麼第一個「if」不會被傳遞,因此不會返回零。 while循環簡單地計數前導空格,在這種情況下,whiteSpaceCount = 5。whiteSpaceCount(5)不小於tabLength(4),因此不返回零。 trimToLowerTab爲true,所以whiteSpaceCount減少了whiteSpaceCount(5)%tabLength(4)(1)。因此,whiteSpaceCount最終是4. – wageoghe 2014-01-31 18:42:41

5

我沒有運行任何性能測試,但是這是更少的代碼。

... 

whitespace = line.Length - line.TrimStart(' ').Length; 

... 
+2

注意:您可以刪除''''來獲取所有空格而不是''''。 – 2010-10-11 16:05:10

+0

感謝這就是我正在尋找的 – Nobody 2010-10-11 16:06:08

+0

@rmx - 那麼我先前的評論是沒有意義的。 :)奧斯汀的答案返回一個整數。如果你想把它轉換成實際的空白,你可以'返回新的字符串'('',whitespace);' – 2010-10-11 16:10:44

0

沒有建成,但如何:

var result = line.TakeWhile(x => x == ' '); 
if (trimToLowerTab) 
    result = result.Skip(result.Count() % 4); 
return new string(result.ToArray()); 
2

您應該使用Char.IsWhiteSpace,而不是與' '比較,通常。並非所有的「空格」都是' '

2

我確定沒有內置任何內容,但是如果您對它們感到滿意,可以使用正則表達式來執行此操作。這匹配任何行開頭的空格:

public static string GetLeadingWhitespace(string line) 
{ 
    return Regex.Match(line, @"^([\s]+)").Groups[1].Value; 
} 

注意:這不會像簡單循環那樣執行。我只想跟你的實施一起去。

+0

另外:我忽略了「最接近4個部分的倍數」,原來的問題聲稱它並不重要。 – steinar 2010-10-11 16:25:11