nth character (1 Viewer)

dgkindy

Registered User.
Local time
Today, 12:47
Joined
Feb 22, 2007
Messages
34
I have recently seen a function that will find the position of the nth character in a string but cannot remember what it was?
 

dgkindy

Registered User.
Local time
Today, 12:47
Joined
Feb 22, 2007
Messages
34
x = mid(mystring,n,1)
n represents the starting point of the function, while the 1 represents the lenght of the trimming

I will provide an example

C:mydocuments\floder1\folder2\folder3

I want to locate the second \ in the string
 

LPurvis

AWF VIP
Local time
Today, 17:47
Joined
Jun 16, 2008
Messages
1,269
George offered what he did because of the phrase "find the position of the nth character in a string". It doesn't make much sense as is.
Technically - the answer to that question is N. :)

But you're wanting the position of the nth occurance of a specified character.
Why do you want that character's position?

Here's a non-rigourously tested different take on the task than normal.
Code:
Function fGetCharPos(strText As String, strChar As String, Optional intPos As Integer = 1)
On Error Resume Next
 
    Dim strArr() As String
 
    strArr = Split(strText, strChar)
    ReDim Preserve strArr(intPos - 1)
    fGetCharPos = Len(Join(strArr)) + (Len(strChar) - 1) * (intPos - 1) + 1
 
End Function
 

dgkindy

Registered User.
Local time
Today, 12:47
Joined
Feb 22, 2007
Messages
34
Why I want to be able to do is so that I can easily trim the directory string at the nth \ and then add in a new directory location

C:mydocuments\floder1\folder2\folder3
C:mydocuments\floder1\folder2\folder4
C:mydocuments\floder1\folder5
 

LPurvis

AWF VIP
Local time
Today, 17:47
Joined
Jun 16, 2008
Messages
1,269
Fair enough - the function as suggested should be enough for you.
Or you could amend it to return the string to the left, rather than just the position.
e.g.
Code:
Function fGetChars(strText As String, strChar As String, Optional intPos As Integer = 1)
On Error Resume Next
 
    Dim strArr() As String
 
    strArr = Split(strText, strChar)
    ReDim Preserve strArr(intPos - 1)
    fGetChars = Join(strArr, strChar) & strChar
 
End Function

Giving:
?fGetChars("C:\mydocuments\folder1\folder2\folder4","\",3)
C:\mydocuments\folder1\
 

ions

Access User
Local time
Today, 09:47
Joined
May 23, 2004
Messages
791
what about two nested inStr()? find the first postion using inStr (use 1 as the start parameter), then if it finds it alter your start parameter to be the result of the first inStr.

Code:
If instr(1,"a","abcabc")  > 0 then
       intposition = instr(instr(1,"a","abcabc")+ 1, "a","abcabc")
end if
 

LPurvis

AWF VIP
Local time
Today, 17:47
Joined
Jun 16, 2008
Messages
1,269
Yeah. Using InStr would a more traditional method - though, obviously, you'd have to write a function to call it iteratively on the string provided for the required number of occurances (hard coding it to two levels - or more - would be much too limited as a solution).

I offered the function solution as presented mainly because Split has become the common trend for more abbreviated code (i.e. it's cooler ;-).
 

Users who are viewing this thread

Top Bottom