2013-10-08 2 views
2

사용자가 텍스트를 입력 할 수있는 텍스트 상자가 있습니다. 현재이 코드 사용하는 모든 공간과 빈 줄을 제거 해요 :TextBox에서 선행 및 꼬리 공백과 빈 줄을 더 잘 제거합니다.

private void RemoveSpacesAndEmptyLines() 
{ 
    textBox.Lines = textBox.Lines.Where(val => val.Trim().Length != 0).ToArray(); 
    textBox.Lines = textBox.Lines.Select(c => c.Trim()).ToArray(); 
} 

그러나이 가능한 단 하나의 호출을 할 수 있습니까?
공백을 제외하고 공백을 제외하고 모든 공백을 제거하는 선만 필요합니다.

답변

3

한 번만 전화를 걸 수 있습니까?

물론

, 당신은 Where을 체인 수 있기 때문에 그리고 Select :

textBox.Lines 
    .Where(val => val.Trim().Length != 0) 
    .Select(c => c.Trim()).ToArray(); 
+1

내가이 방법을 수행 할 수 있다는 :) 나는 체인 잊어 생각하지 않았다. 감사! – Misiu

2
textBox.Lines = textBox.Lines 
    .Select(l => l.Trim()) 
    .Where(l => !string.IsNullOrEmpty(l)) 
    .ToArray(); 
-1
function trim (el) { 
    el.value = el.value. 
     replace (/(^\s*)|(\s*$)/, ""). // removes leading and trailing spaces 
     replace (/[ ]{2,}/gi," ").  // replaces multiple spaces with one space 
     replace (/\n +/,"\n");   // Removes spaces after newlines 
    return; 
}​ 


onkeypress="return trim(this)" 
+0

이것이 올바른 ans입니다. – bharati

+0

흥미로운 C#이 있습니다. – Greg

관련 문제