Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Recurrence of character in string


Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2003, 02:37 PM
TiBaal89's Avatar
TiBaal89 TiBaal89 is offline
Centurion
 
Join Date: Jun 2003
Location: Milwaukee
Posts: 126
Default Recurrence of character in string


I need to find out how many times a particular character is present in a string. Its part of a larger program that generates HTML. I have a string thats tens of thousands of characters long and need to know how many times the '<' symbol appears in it. Any suggestions on a function for this?

i haven't been able to come up with anything using the basic string parsing commands... there's probably a command I'm not aware of that would do this... help!
Reply With Quote
  #2  
Old 06-23-2003, 02:57 PM
Csharp's Avatar
Csharp Csharp is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Location: Ashby, Leicestershire.
Posts: 967
Default

Code:
Private Sub Command1_Click() Dim str As String, i As Integer, x As Integer str = "<testing<1234<<5678><abc123<" For i = 1 To Len(str) If Mid(str, i, 1) = "<" Then x = x + 1 End If Next MsgBox "the number of occurences of < in the string is :" & x End Sub
__________________
~~ please don't PM me regarding code, I only reply to personnal messages ~~
Reply With Quote
  #3  
Old 06-23-2003, 03:09 PM
TiBaal89's Avatar
TiBaal89 TiBaal89 is offline
Centurion
 
Join Date: Jun 2003
Location: Milwaukee
Posts: 126
Default

Wow, that never occured to me... Thanks!
Reply With Quote
  #4  
Old 06-23-2003, 03:16 PM
TiBaal89's Avatar
TiBaal89 TiBaal89 is offline
Centurion
 
Join Date: Jun 2003
Location: Milwaukee
Posts: 126
Default

Hmm... that caused major overflow. i'm going to try to amend it, anyone have any tips?
Reply With Quote
  #5  
Old 06-23-2003, 04:04 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 19,054
Default

Change the Integer to Long
Reply With Quote
  #6  
Old 06-23-2003, 04:40 PM
Legend Legend is offline
Contributor
 
Join Date: Dec 2002
Posts: 542
Default

This might be a bit more efficient for very long string:

Code:
Function CountChar(strChar As String, strInput As String) as long Dim lngPos As Long Dim lngCount As Long Dim lngLast As Long lngLast = 1 lngPos = -1 While lngPos <> 0 lngPos = InStr(lngLast, strInput, strChar) If lngPos Then lngCount = lngCount + 1 lngLast = lngPos + 1 End If Wend CountChar = lngCount End Function
Reply With Quote
Reply


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 Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a file in dat question ? murphys General 5 11-06-2002 08:34 PM
Open an associated Text Document knoxitis General 19 05-01-2002 11:26 PM
Shareware Registry Protection Technigue karachi999 General 2 01-21-2002 02:40 PM
Loop to format file kingesk General 2 09-10-2000 05:01 PM
File format problem kingesk General 1 09-08-2000 04:52 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->