2011-04-27 55 views
1

我目前正在使用Visual Basic進行腳本編制的新聞圖形程序。我的挑戰是這樣的:如何在「x」字符後插入換行符

我有一個.xml文件,其中包含一些數據,其中一個需要添加到圖形(從我們的網站新聞標題)。這些標題對於圖形來說太長,然而,需要爲它們添加換行符。我已經成功地通過圖形軟件的其餘部分將此標題寫入腳本,並且它的temp.txt名稱是BodyTxt(i).Text(其中(i)是腳本的另一部分的循環的一部分,但將總是等於1,2或3)。在35個字符後,我需要換行符。這樣做最簡單的方法是什麼?

爲了將來的參考,我可以看到這是爲了在網頁中創建一個類似的腳本,以便自動填充來自RSS或.xml提要的數據字段而不會中斷模板並強制縮小字體以適應整個領域,或在一個詞的中間創建換行符。

回答

3

這是你在找什麼?


Sub Main() 
    Dim testMessage As String 

    testMessage = "For future reference, I could see this being used in order to create a similar script within a web page in order to automatically populate data fields from an RSS or .xml feed without breaking the template and either forcing a font to shrink to fit the entire field, or creating a line break in the middle of a word." 

    PrintMessage(testMessage, 30) 

    Console.ReadLine() 
End Sub 

Sub PrintMessage(Message As String, Length As Integer) 
    Dim currentLength = 0 

    Dim words As Array 

    words = Split(Message, " ") 

    For Each word As String In words 
     If currentLength + word.Length > Length Then 
      Console.Write(ControlChars.Tab & currentLength)           
      Console.WriteLine() 
      currentLength = 0 
     End If 

     Console.Write(word & " ") 
     currentLength += word.Length 
    Next 
     Console.Write(ControlChars.Tab & currentLength) 
End Sub 

產生這樣的輸出:

For future reference, I could see  28 
this being used in order to create a 29 
similar script within a web page in 29 
order to automatically populate  28 
data fields from an RSS or .xml feed 29 
without breaking the template and  29 
either forcing a font to shrink to 28 
fit the entire field, or creating a 29 
line break in the middle of a word. 29 
相關問題