Help Newbie - String stripping...? 
Author Message
 Help Newbie - String stripping...?

I am trying to strip of the beginning of a string...  I am having problems.
I want to search for the last "\" in the string, then strip it and
everything before it off.  The string that I am stripping is
"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
with:

Function GetUserName(UserString$) As String
Dim p%
NDSName = UserString
For p% = Len(UserString) To 0 Step -1
    If Mid$(UserString, p%, 1) = "\" Then
        NDSName = Left$(UserString, p%)
        Exit Function
    End If
Next p%
End Function



Sat, 15 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?

I am trying to strip of the beginning of a string...  I am having problems.
I want to search for the last "\" in the string, then strip it and
everything before it off.  The string that I am stripping is
"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
with:

Function GetUserName(UserString$) As String
Dim p%
NDSName = UserString
For p% = Len(UserString) To 0 Step -1
    If Mid$(UserString, p%, 1) = "\" Then
        NDSName = Left$(UserString, p%)
        Exit Function
    End If
Next p%
End Function

By the way, the string is dynamic and will vary in length for each
runtime...



Sat, 15 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?

Quote:

>I am trying to strip of the beginning of a string...  I am having problems.
>I want to search for the last "\" in the string, then strip it and
>everything before it off.  The string that I am stripping is
>"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
>with:

There are alot of ways that you could do this but using you code...

Quote:
>Function GetUserName(UserString$) As String
>Dim p%
>NDSName = UserString
>For p% = Len(UserString) To 0 Step -1
>    If Mid$(UserString, p%, 1) = "\" Then

At this point p% contains the number of you don't want so take the total
number of characters in the string subtract the ones you don't want, the
difference is the number of characters that you want to keep from the right
side of the string.

           NDSName = Right$(UserString, Len(UserString) - p%)

Now so the function will return the string...

            GetUserName = NDSName

delete this line.

Quote:
>        NDSName = Left$(UserString, p%)
>        Exit Function
>    End If
>Next p%
>End Function

>By the way, the string is dynamic and will vary in length for each
>runtime...



Sat, 15 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?

Quote:

>I am trying to strip of the beginning of a string...  I am having problems.
>I want to search for the last "\" in the string, then strip it and
>everything before it off.  The string that I am stripping is
>"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
>with:

>Function GetUserName(UserString$) As String
>Dim p%
>NDSName = UserString
>For p% = Len(UserString) To 0 Step -1
>    If Mid$(UserString, p%, 1) = "\" Then
>        NDSName = Left$(UserString, p%)
>        Exit Function
>    End If
>Next p%
>End Function

Jody ,
Try:
Option Explicit
'NOTE THERE IS A WINDOWS API CALL GetUserName,
so you may not want to use that as a Function name:

Function GetUserNameVB(ByVal UserString As String) As String
Dim p As Long
    For p = Len(UserString) To 1 Step -1
        If Mid$(UserString, p, 1) = "\" Then
            GetUserNameVB = Mid$(UserString, p + 1)
            Exit Function
        End If
    Next   'p
End Function

Private Sub Command1_Click()
Dim strX As String
Text1 = GetUserNameVB(Text1)
End Sub

Neila



Sat, 15 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?
Hi,

Try this

Function PassString(ByVal Inpt as string) as string

    Dim Pos as Integer

    pos=Instr(Inpt, "\")
    Do Until Pos=0
        Inpt=Mid(Inpt, Pos+1)
        pos=Instr(Inpt, "\")
    Loop
    PassString=Inpt

End Function

Regards

Trevor

Quote:

>I am trying to strip of the beginning of a string...  I am having problems.
>I want to search for the last "\" in the string, then strip it and
>everything before it off.  The string that I am stripping is
>"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working



Sun, 16 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?
see the help for mid and instr.  if you have vb6, you might want to look at
instrrev.
Quote:

>I am trying to strip of the beginning of a string...  I am having problems.
>I want to search for the last "\" in the string, then strip it and
>everything before it off.  The string that I am stripping is
>"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
>with:

>Function GetUserName(UserString$) As String
>Dim p%
>NDSName = UserString
>For p% = Len(UserString) To 0 Step -1
>    If Mid$(UserString, p%, 1) = "\" Then
>        NDSName = Left$(UserString, p%)
>        Exit Function
>    End If
>Next p%
>End Function

>By the way, the string is dynamic and will vary in length for each
>runtime...



Sun, 16 Sep 2001 03:00:00 GMT  
 Help Newbie - String stripping...?
Hello, Jody

Your GetUserName() function will return an empty string, because you have
forgotten to assign GetUserName within the function.   I presume that what
you want to return from this function is the substring to the RIGHT of the
final backslash, that is, "Greeneje" in this case?  If so,

Function GetUserName(UserString$) As String
   Dim p%
   For p% = Len(UserString) To 0 Step -1
      If Mid$(UserString, p%, 1) = "\" Then
         GetUserName = Mid$(UserString, p%+1)
         Exit Function
      End If
   Next p%
End Function

If I have misinterpreted what you meant by string stripping, and you perhaps
wanted the stuff to the left (the path excluding the file name) then you
would just return  GetUserName = Left$(UserString, p%)   instead.

Cheers

Mark Moorhouse

======================================================

Quote:

>I am trying to strip of the beginning of a string...  I am having problems.
>I want to search for the last "\" in the string, then strip it and
>everything before it off.  The string that I am stripping is
>"NDS:\\Tree\Cont1\Cont2\Cont3\Greeneje" and here is the code I am working
>with:

>Function GetUserName(UserString$) As String
>Dim p%
>NDSName = UserString
>For p% = Len(UserString) To 0 Step -1
>    If Mid$(UserString, p%, 1) = "\" Then
>        NDSName = Left$(UserString, p%)
>        Exit Function
>    End If
>Next p%
>End Function



Mon, 17 Sep 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Pls Help Newbie: Stripping Strings of CR and LF

2. stripping a string - Newbie

3. Newbie: stripping carriage returns from a string

4. Tab Strip help needed, Newbie?

5. HELP! Stripping NON ALPHA characters from a string

6. Strip attachment off Email (Sorry newbie)

7. Newbie: How to select a tab on a tab strip

8. newbie: strip the carriage returns from a text file

9. NEWBIE:Function to strip off unwanted characters

10. Strip Spaces from string

11. Stripping vbCrLf from strings

12. Strip spaces from string

 

 
Powered by phpBB® Forum Software