How to find the first occurrence in string vbscript

advertisements

I need to check the last char in string and if he don't space(" ") or new line(vbCrLf),i need to find the first occurrence in string.

my code:

Function checkTextFunction( i_valueCheck)
    Dim whereSpace
    Dim WhereNewLine
    Dim indexOFirstSpaceFromTheEnd
    If Right(i_valueCheck,1) = " " Or Right(i_valueCheck,1) = vbCrLf Then
        indexOFirstSpaceFromTheEnd = -1
    Else
        whereSpace = InStrRev(i_valueCheck, " ", -1,0)
        WhereNewLine = InStrRev(i_valueCheck, vbCrLf, -1,0)
        If whereSpace < WhereNewLine Then
            indexOFirstSpaceFromTheEnd = whereSpace
        Else
            indexOFirstSpaceFromTheEnd = WhereNewLine
        End If
    End IF
    WriteLogFileLine "indexOFirstSpaceFromTheEnd: " & indexOFirstSpaceFromTheEnd & " Right(i_valueCheck,1): " & Right(i_valueCheck,1) & vbCrLf & vbCrLf
    checkTextFunction = indexOFirstSpaceFromTheEnd
End Function

but i don't find the first occurrence with the lines

    whereSpace = InStrRev(i_valueCheck, " ", 1)
    WhereNewLine = InStrRev(i_valueCheck, vbCrLf,1)

i get always 0.

Someone have idea?

Thanks,

Tal


  1. vbCrLf is actually 2 characters, so you want to specify Right(i_valueCheck, 2) = vbCrLf
  2. If the character is not in the string, then InStrRev will return 0.. which would mean it is always the first occurrence. The second If statement should be a little more explicit in terms of the values of the variables.
  3. Since you're not initializing indexOFirstSpaceFromTheEnd... If there are no vbCrLf or " " characters- it will remain empty. Therefore you should add a check in order to return a value every time... regardless of the string. I picked one million arbitrarily.
Function checkTextFunction(ByRef i_valueCheck)
    Dim whereSpace
    Dim whereNewLine
    Dim indexOFirstSpaceFromTheEnd

    If Right(i_valueCheck,1) = " " Or Right(i_valueCheck,2) = vbCrLf Then
        indexOFirstSpaceFromTheEnd = -1
    Else
        whereSpace = InStrRev(i_valueCheck, " ", -1, 0)
        whereNewLine = InStrRev(i_valueCheck, vbCrLf, -1, 0)

        If whereSpace > 0 And (whereSpace < whereNewLine Or whereNewLine = 0) Then
            indexOFirstSpaceFromTheEnd = whereSpace
        ElseIf whereNewLine > 0 And (whereNewLine < whereSpace Or whereSpace = 0) Then
            indexOFirstSpaceFromTheEnd = whereNewLine
        End If
    End If

    If indexOFirstSpaceFromTheEnd = vbEmpty Then indexOFirstSpaceFromTheEnd = 1000000

    WriteLogFileLine "indexOFirstSpaceFromTheEnd: " & _
                      indexOFirstSpaceFromTheEnd    & _
                      " Right(i_valueCheck,1): "    & _
                        Right(i_valueCheck,1)       & vbCrLf & vbCrLf

    checkTextFunction = indexOFirstSpaceFromTheEnd
End Function

This should help you get started... realistically- if you want to find the first occurrence of vbCrLf or " " then you're going to have to operate a While loop in order to keep checking (with InStrRev) that the index is actually the last occurrence.

For example: with the code above... look at the output of checkTextFunction(Space(2) & "test")