2009-09-29 16 views
0

메신저는 영수증 레이아웃을 사용하고 길이가 24 문자보다 길면 제품 설명 텍스트를 2 줄로 나눕니다.문자열에서 2 단어를 추출하십시오.

내 최초의 솔루션은 다음과 같이이었다 :

If Row.Description.Length >= 24 Then 
TextToPrint &= Row.Description.Substring(0, 24) & "  $100" 
TextToPrint &= Row.Description.Substring(24) & vbNewLine 
else 
TextToPrint &= Row.Description & filloutFunction(Row.Description.length) &"  $100" & vbNewLine 
end if 

하지만이 결과를 제공합니다.

A product with a long na $100 
me that doesn't fit   

정상적으로 보이는 것처럼 설명을 구분하는 기능을 만드는 방법을 알아낼 수 없습니다.

A product with a long  $100 
name that doesn't fit 

희망 난/자신을 분명하게 :

답변

0

내 첫 샷,

private static List<string> SplitSentence(string sentence, int count) 
{ 
    if(sentence.Length <= count) 
    { 
     return new List<string>() 
       { 
        sentence 
       }; 
    } 

    string extract = sentence.Substring(0, sentence.Substring(0, count).LastIndexOfAny(new[] 
                         { 
                          ' ' 
                         })); 

    List<string> list = SplitSentence(sentence.Remove(0, extract.Length), count); 

    list.Insert(0, extract.Trim()); 

    return list; 
} 

등 : 나는 그것을 최적화 할 수 있다고 생각

string sentence = "A product with a long name that doesn't fit"; 

List<string> sentences = SplitSentence(sentence, 24); 
sentences[0] = sentences[0] + "  $100"; 

.

+1

고맙습니다! 나는이 유용한뿐만 아니라 발견! – Alexander

1

그 이상 (24)는 다음 decrementally 점 (23)의 공백 문자를 찾을 경우. 찾으면 해당 위치에서 문자열을 나눕니다. 그 '칼럼'시스템은 당신이보기에는 꽤 고약 해 보인다. 이 같은

+0

영수증 프린터. 용지의 한 행의 너비는 42 자입니다. "오른쪽 배열" – Alexander

+0

아, 그때 이해할 수있는 "금액 - 열"remaning 18 문자가 필요합니다. – UpTheCreek

0

뭔가 작업을해야합니다 :

Dim yourString = "This is a pretty ugly test which should be long enough" 
Dim inserted As Boolean = False 
For pos As Integer = 24 To 0 Step -1 
    If Not inserted AndAlso yourString(pos) = " "c Then 
     yourString = yourString.Substring(0, pos + 1) & Environment.NewLine & yourString.Substring(pos + 1) 
     inserted = True 
    End If 
Next 
+0

고마워요! 그 완벽하게 작동했습니다! – Alexander

관련 문제