Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 9th, 2007, 8:15 PM   #1
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
indexof

Does VB05 have some kind of next occurrence for indexof in strings? I only see index of (first occurence) and lastindexof (last occurrence), but I find nothing to find next occurrence.
Jabo is offline   Reply With Quote
Old Nov 9th, 2007, 8:26 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: indexof

VB 5? Even I can't remember that far back. I think you'll want to look for string methods in general and see if you recognize something useful.

There are almost always methods for indexing a particular character (string [5], charAt [7], etc.) There's also generally a method for finding an occurrence of some particular character within the string.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 9th, 2007, 8:43 PM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: indexof

I'm sorry, I didn't make myself clear. I meant VB 2005. String has an indexof function, and a lastindexof function, but I don't see anything to iterate through all occurrences of a single char. I guess that's an oversight on their part. I'm gonna hate having to iterate them myself, as my code is starting to look a little bloated and this won't help it any.
Jabo is offline   Reply With Quote
Old Nov 9th, 2007, 9:24 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: indexof

Ahhhh. You can get the character (or substring) at any position by using substr. You can search for a character (or substring) using inStr. Again, I'd recommend you look at the methods that are members of the class. These people set out to write things that are generally useful. They might not choose to restrict them unduly, or name them what one expects.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 10th, 2007, 12:47 AM   #5
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: indexof

Thanks, DaWei.

I guess the best place to look would be in the MSDN for the string class then? I've looked at everything that pops up when you hit the period after the string, but I guess it could be hidden deeper like under string.chars.

To show the bloat, here's the sub, and the else statement at the end is where I really need this function.
vb Syntax (Toggle Plain Text)
  1. Private Sub cmdChange_Click(ByVal sender As System.Object, _
  2. ByVal e As System.EventArgs) Handles cmdChange.Click
  3. Dim dex, sstart, length, ssdex(9) As Integer
  4. Dim sstring As String
  5. Dim chstring() As Char
  6.  
  7. 'check that there are files to manipulate
  8. If lboSelLst.Items.Count = 0 Then
  9. mbox.txtMsg.Text = "There are no files selected to make changes to." _
  10. & Chr(13) & Chr(10) & "Select some files first."
  11. mbox.Show()
  12. Else
  13. 'check which option is checked
  14. If rbnXt.Checked = True Then
  15. 'check if there is a period and rename the file with "_ext.<new_ext>"
  16. For x As Integer = 0 To lboSelLst.Items.Count - 1
  17. If lboSelLst.Items.Item(x).ToString.Contains(".") Then
  18. dex = lboSelLst.Items.Item(x).ToString.IndexOf(".")
  19. lboSelLst.Items.Item(x) = lboSelLst.Items.Item(x).ToString.Substring _
  20. (0, dex) & "_" & lboSelLst.Items.Item(x).ToString.Substring _
  21. (dex + 1, lboSelLst.Items.Item(x).ToString.Length - (dex + 1)) _
  22. & "." & cboOptions.Text
  23. Else
  24. 'if no period, just add period to end and new_ext
  25. dex = lboSelLst.Items.Item(x).ToString.Length
  26. lboSelLst.Items.Item(x) = lboSelLst.Items.Item(x).ToString.Substring _
  27. (0, dex) & "." & cboOptions.Text
  28. End If
  29. Next
  30. ElseIf rbnName.Checked = True Then
  31. For x As Integer = 0 To lboSelLst.Items.Count - 1
  32. 'check for period, append extenstion
  33. If lboSelLst.Items.Item(x).ToString.Contains(".") Then
  34. dex = lboSelLst.Items.Item(x).ToString.IndexOf(".")
  35. lboSelLst.Items.Item(x) = lboSelLst.Items.Item(x) _
  36. .ToString.Substring(0, dex) & "_" & cboOptions.Text & lboSelLst. _
  37. Items.Item(x).ToString.Substring(dex, lboSelLst.Items.Item(x). _
  38. ToString.Length - dex)
  39. Else
  40. 'or add text to end if no period
  41. dex = lboSelLst.Items.Item(x).ToString.Length
  42. lboSelLst.Items.Item(x) &= "_" & cboOptions.Text
  43. End If
  44. Next
  45. ElseIf rbnDate.Checked = True Then
  46. For x As Integer = 0 To lboSelLst.Items.Count - 1
  47. 'check for period, append date
  48. If lboSelLst.Items.Item(x).ToString.Contains(".") Then
  49. dex = lboSelLst.Items.Item(x).ToString.IndexOf(".")
  50. lboSelLst.Items.Item(x) = lboSelLst.Items.Item(x) _
  51. .ToString.Substring(0, dex) & "_" & cboOptions.Text & lboSelLst. _
  52. Items.Item(x).ToString.Substring(dex, lboSelLst.Items.Item(x). _
  53. ToString.Length - dex)
  54. Else
  55. 'or if no period, append date to end of filename
  56. lboSelLst.Items.Item(x) &= "_" & cboOptions.Text
  57. End If
  58. Next
  59. ElseIf rbnChXt.Checked = True Then
  60. For x As Integer = 0 To lboSelLst.Items.Count - 1
  61. 'check for period, change extension
  62. If lboSelLst.Items.Item(x).ToString.Contains(".") Then
  63. dex = lboSelLst.Items.Item(x).ToString.IndexOf(".")
  64. lboSelLst.Items.Item(x) = lboSelLst.Items.Item(x). _
  65. ToString.Substring(0, dex + 1) & cboOptions.Text
  66. Else
  67. 'if no period, add period and extension
  68. lboSelLst.Items.Item(x) &= "." & cboOptions.Text
  69. End If
  70. Next
  71. Else
  72. sstring = cboOptions.Text.Trim
  73. length = sstring.Length
  74. chstring = sstring
  75.  
  76. 'ssdex is used to hold indexes of occurrences of the first
  77. 'letter in chstring, this initializes all values to -1
  78. 'for each item in the array.
  79. For z As Integer = 0 To 9
  80. ssdex(z) = -1
  81. Next
  82.  
  83. 'for each character in the string...
  84. For x As Integer = 0 To lboSelLst.Items.Count - 1
  85. 'find the period and set dex to it's index
  86. If lboSelLst.Items.Item(x).ToString.Contains(".") Then
  87. dex = lboSelLst.Items.Item(x).ToString.IndexOf(".")
  88. Else
  89. 'or to the end of the string if no period
  90. dex = lboSelLst.Items.Item(x).ToString.Length
  91. End If
  92.  
  93. 'if the string contains the string entered by user
  94. If lboSelLst.Items.Item(x).ToString.Contains(sstring) Then
  95. 'initialize z, which will be the number of occurrences
  96. 'of the first char in chstring found in the filename
  97. Dim z As Integer = 0
  98. For ch As Integer = 0 To lboSelLst.Items.Item(x).ToString.Length - 1
  99. Dim ltr As Char
  100. ltr = lboSelLst.Items.Item(x).ToString.Chars(ch)
  101. If ltr.Equals(chstring(0)) Then
  102. 'if equal, set ssdex to the character's index number
  103. 'and increment the index of ssdex
  104. ssdex(z) = ch
  105. z += 1
  106. End If
  107. Next
  108.  
  109. 'for the number of occurrences of the char found
  110. 'set the starting index in the filename
  111. For y As Integer = 0 To z - 1
  112. sstart = ssdex(y)
  113. 'make sure program isn't going past the period
  114. If Not (sstart + length > dex) Then
  115. 'check that substring is equal to search string
  116. If lboSelLst.Items.Item(x).ToString.Substring _
  117. (sstart, length).Equals(sstring) Then
  118. 'and change name in list
  119. lboSelLst.Items.Item(x) = _
  120. lboSelLst.Items.Item(x).Substring _
  121. (0, sstart) & lboSelLst.Items.Item(x).ToString.Substring _
  122. (sstart + length, lboSelLst.Items.Item(x) _
  123. .ToString.Length - (sstart + length))
  124. End If
  125. End If
  126. Next
  127. End If
  128. Next
  129. End If
  130. End If
  131. End Sub

Last edited by Jabo; Nov 10th, 2007 at 1:04 AM.
Jabo is offline   Reply With Quote
Old Nov 10th, 2007, 1:30 AM   #6
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: indexof

Correction, the next to last else statement
Jabo is offline   Reply With Quote
Old Nov 10th, 2007, 3:01 AM   #7
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: indexof

The way I've done this, seems to be the most round-about way one could possibly do it. I will try to find a better way

Starting line# 71 is the else I'm talking about.

Last edited by Jabo; Nov 10th, 2007 at 3:23 AM.
Jabo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC