Close

Results 1 to 8 of 8
  1. #1
    DF VIP Member keyscoob's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    1,222
    Thanks
    0
    Thanked:        2
    Karma Level
    391

    Len() function and spaces

    I'm using the Len() function to count the number of letters that have been pasted into a Text Box from another application and display the total letter count within a label.

    Obviously Len(Text1.Text) will count spaces between words/letters as well as the actual letters. Does anyone now how I can avoid counting the spaces so that my label caption shows only the total number of letters pasted?

    Thanks...
    Last edited by keyscoob; 10th October 2002 at 11:25 AM.

  2. #2
    DF VIP Member salvadorescobar's Avatar
    Join Date
    Dec 2000
    Location
    Huddersfield
    Posts
    951
    Thanks
    0
    Thanked:        0
    Karma Level
    346

    Default

    I don' t know what language you are taling about here (is this VBScript?).

    You are not wanting to Trim the spaces at the beginning and end, but count all spaces right?

    try something like this...

    var strCount = Len(Text1.Text);
    var strSpaceCount = 0;


    // now loop through the string and count the spaces
    for (i=0;i<strCount;i++) {
    if (Text1.Text[i] == Chr(32)) { // 32 is the ASCII value of a space
    strSpaceCount++;
    }
    }

    // finally subtract the no of spaces from the length of the whole string
    var strMinusSpacesCount = strCount - strSpaceCount;

    There may be a function to count the occurance of chars in a string (I know there is in a number of languages) , however this algorithm will work in many languages.

    I hope this helps.
    'If we aren't meant to eat animals, then why are they made out of meat?'
    Anon.

  3. #3
    DF VIP Member keyscoob's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    1,222
    Thanks
    0
    Thanked:        2
    Karma Level
    391

    Default

    Hi,

    Thanks for the feedback. I an using VB (should have said) and think I have it sorted now.

    Thanks again...

  4. #4
    DF VIP Member keyscoob's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    1,222
    Thanks
    0
    Thanked:        2
    Karma Level
    391

    Default

    Did this to sort my problem:

    Private Sub Command1_Click()

    'Label1.Caption = Len(Text1.Text)

    Dim strToSearch As String
    Dim intLength As Integer
    Dim intPosition As Integer
    Dim intCount As Integer
    Dim i As Integer
    Dim j As Integer

    Dim arrTotals(26) As Integer
    Dim arrPercentages(26) As Single
    Dim strMessage As String

    'put the text from the text box into a string
    strToSearch = Text1.Text

    'get the string length
    intLength = Len(strToSearch)

    'only make checks if it is not an empty string
    'otherwise you will get an overflow error
    If intLength > 0 Then

    For i = 64 To 90
    'for each letter
    intCount = 0
    Do
    'check the number of occurances
    intPosition = InStr(intPosition + 1, UCase(strToSearch), Chr(i))
    If intPosition > 0 Then
    intCount = intCount + 1
    Else
    Exit Do
    End If
    Loop
    arrTotals(i - 64) = intCount
    arrPercentages(i - 64) = Format((intCount / intLength) * 100, "0.00")
    Next i

    'display results
    strMessage = ""

    For i = 1 To 26
    strMessage = strMessage & Chr(i + 64) & " " & "Appears " & arrTotals(i) & " Times" & " And Accounts For " & arrPercentages(i) & "% Of The Total Characters" & ", "
    Next i

    End If

    Text2.Text = strMessage

    End Sub


    Another Question!!

    My textbox has 26 sets of results displayed within it by using:

    For i = 1 To 26
    strMessage = strMessage & Chr(i + 64) & " " & "Appears " & arrTotals(i) & " Times" & " And Accounts For " & arrPercentages(i) & "% Of The Total Characters" & ", "
    Next i

    I have the TextBox set to multiline with scroll bars. How do I get the results for each loop of the above For-Next to be displayed on a new line within the text box????


  5. #5
    DF Member Plasma's Avatar
    Join Date
    Oct 2002
    Location
    Planet Earth
    Posts
    37
    Thanks
    0
    Thanked:        0
    Karma Level
    0

    Default

    When you add each result to the Text box also do a & vbNewLine

    eg....

    Text1.Text = Result1 & vbNewLine

    Hope this helps, let me know if not!

    Plasma

  6. #6
    DF VIP Member keyscoob's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    1,222
    Thanks
    0
    Thanked:        2
    Karma Level
    391

    Default

    Hi,m8

    Thanks for the feedback. In the end I used the vbCrLf function in the following format.

    Text2.Text = strMessage + vbCrLf & "The total number of Letters is " & intLength _
    & " (excluding spaces)" & vbCrLf

    Many thanks for your feedback the app now works virtually how I need it to.



  7. #7
    DF VIP Member graham.edmon's Avatar
    Join Date
    May 2001
    Location
    Edinburgh
    Posts
    263
    Thanks
    0
    Thanked:        0
    Karma Level
    290

    Default

    why not just count the spaces and subtract it from the length.. it is slow an unnecessary to go through every character of the string...

    something like this

    Dim lSpaces
    Dim lIndex

    lSpaces = 0
    lIndex = Instr(1, sString, " ")
    Do Until lIndex = 0
    lSpaces = lSpaces + 1
    lIndex = Instr(lIndex, sString, " ")
    Loop

    Msgbox "Chars is string are " & Len(sString) - lSpaces

  8. #8
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    389

    Default

    it is slow an unnecessary to go through every character of the string
    Mind you the Instr function is going thru the list as well ... albeit more efficiently than VB code.

Similar Threads

  1. Fines for improper use of Designated Parking Spaces
    By Tommy J in forum News & Current Affairs
    Replies: 32
    Last Post: 28th September 2007, 04:57 PM
  2. New Updates for Windows Live Spaces
    By Raptor in forum Microsoft Windows XP & Vista
    Replies: 0
    Last Post: 20th July 2007, 01:30 AM
  3. New Live Spaces Update - with welcome improvements
    By Raptor in forum Microsoft Windows XP & Vista
    Replies: 0
    Last Post: 28th April 2007, 05:12 PM
  4. Tescos parking spaces need to be longer!
    By zelnik in forum Cars & Motorbikes
    Replies: 16
    Last Post: 16th April 2006, 10:07 PM
  5. Msn Spaces
    By Sega rossi in forum The Dog and Duck
    Replies: 8
    Last Post: 10th April 2005, 12:02 PM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •