advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 3.1/5 | Rate this item | 7 users have rated this item.
Expertise: Advanced
Language: .NET
January 6, 2009
Trimming Items in a String Array
When you use a For Each loop to iterate through a String Array and perform some action with each item, the change doesn't affect the String Array itself. Consider the following code:

Dim strArray() As String = {"First Item", "Second Item", "Third Item"}

' First we are trying to add some more 
' text with each item in array
For Each strItem As String In strArray
    strItem &= " some data"
Next

' Now again iterate through this array to see 
' whether the changes has been made or not
For Each strItem As String In strArray
    Debug.Print(strItem)
Next
The output of the above code will be:

First Item
Second Item
Third Item
As you can see in the output, there is no change in items. Each time the value is copied into the variable, it changes the variable, not the Array item. To make changes in each item, use the For Next loop. For example:

Dim strArray() As String = {"First Item", "Second Item", "Third Item"} 

' First we are trying to add some more ' text with each item in array 
For i As Integer = 0 To strArray.Length - 1
    strArray(i) &= " some data"
Next

' Now again iterate through this array to see
' whether the changes has been made or not
For Each strItem As String In strArray
    Debug.Print(strItem)
Next
The output of the above code will be:

First Item some data
Second Item some data
Third Item some data
Arsalan Tamiz
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs