30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 Strings, Characters and Regular Expressions 659<br />

38 buffer.Insert(0, longValue)<br />

39 buffer.Insert(0, " ")<br />

40 buffer.Insert(0, singleValue)<br />

41 buffer.Insert(0, " ")<br />

42 buffer.Insert(0, doubleValue)<br />

43 buffer.Insert(0, " ")<br />

44<br />

45 output = "buffer after inserts:" & vbCrLf & _<br />

46 buffer.ToString() & vbCrLf & vbCrLf<br />

47<br />

48 buffer.Remove(12, 1) ' delete 5 in 2.5<br />

49 buffer.Remove(2, 4) ' delete .333 in 33.333<br />

50<br />

51 output &= "buffer after Removes:" & vbCrLf & _<br />

52 buffer.ToString()<br />

53<br />

54 MessageBox.Show(output, "Demonstrating StringBuilder " & _<br />

55 "Insert and Remove Methods", MessageBoxBut<strong>to</strong>ns.OK, _<br />

56 MessageBoxIcon.Information)<br />

57 End Sub ' Main<br />

58<br />

59 End Module ' modBuilderInsertRemove<br />

Fig. 15.14 StringBuilder text insertion and removal (part 2 of 2).<br />

Another useful method included with StringBuilder is Replace. Replace<br />

searches for a specified String or character and substitutes another String or character<br />

in its place. Figure 15.15 demonstrates this method.<br />

1 ' Fig. 15.15: StringBuilderReplace.vb<br />

2 ' Demonstrating method Replace.<br />

3<br />

4 Imports System.Text<br />

5 Imports System.Windows.Forms<br />

6<br />

7 Module modBuilderReplace<br />

8<br />

9 Sub Main()<br />

10 Dim builder1 As StringBuilder = _<br />

11 New StringBuilder("Happy Birthday Jane")<br />

12<br />

13 Dim builder2 As StringBuilder = _<br />

14 New StringBuilder("good bye greg")<br />

Fig. 15.15 StringBuilder text replacement (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!