String

  • Thread starter Thread starter JF Bouthillier
  • Start date Start date
J

JF Bouthillier

Hi all,

Here is an example of my string: 11A-1-21. How can I
separate the three sections to create three different
string (11A, 1, 21)?

The three sections can have variable lenghts...

Thank you so much.
JF
 
You can use the SPLIT routine that will separate the string you pass to
it using the specified separator char and return a string array:

Dim Result as String()
Result = Split("11A-1-21", "-")
Debug.Print Result(0) -> "11A"

Pavel
 
-----Original Message-----
Hi all,

Here is an example of my string: 11A-1-21. How can I
separate the three sections to create three different
string (11A, 1, 21)?

The three sections can have variable lenghts...

Thank you so much.
JF
.
JF -look at midstr if you want to use less steps did this
quick might have to adjust the intx-1 quick help is debug
window then ? plug in a line ie
intx=instr(curstr,"-")
? s1=left(curstr,intx-1
etc.
dim intx as integer,curstr as string,s1s as string,s2 as
string,s3 as string
curstr="11A-1-21"
intx=instr(curstr,"-")
s1=left(curstr,intx-1)
curstr=right(curstr,len(curstr)-intx
intx=instr(curstr,"-")
s2=left(curstr,intx-1)
s3 = right(curstr,len(curstr)-intx-1)
 
Yet another method: -

Function StringTester()
'Declare Variables
Dim LoopLen, MaxError, N, StringLen As Integer
Dim String1, String2, String3, String4 As String
'Declare String
String1 = "11-22-33-44"
'Set Loop Length And String Length
StringLen = Len(String1): LoopLen = Len(String1)
String4 = ""
N = 0
'Declare String Value To Be Replaced
String2 = "-"
'Loop Through String
For N = 1 To LoopLen
'Determine Current Character To Be Checked
String3 = Mid(String1, N, 1)
'Compare Current Character With Declared String
MaxError = StrComp(String2, String3, vbTextCompare)
'If Current Character Matches Declared String Then Replace
& Append To New String
If MaxError = 0 Then
If String4 = "" Then String4 = " " Else String4 =
(String4 & " ")
End If
'If Current Character does not Match Declared String Then
Append To New String
If MaxError <> 0 Then
If String4 = "" Then String4 = String3 Else String4 =
(String4 & String3)
End If
Next N
'Display New String
MsgBox (String4)
End Function
 
You can parse thur your string and look for the special
character "-" using the mid function.

example:

Dim str As String, strBuild As String, strResults As
String, a As String, i As Integer, j As Integer, k As
Integer

str = "11A-1-21"

j = 1
k = 1
i = Len(str)

Do Until j > i
a = Mid(str, j, 1)
If k = 1 Then
strBuild = a
k = k + 1
ElseIf a = "-" Then
strResults = Trim(strBuild)
MsgBox strResults
strResults = ""
strBuild = ""
k = 1
Else
strBuild = strBuild & a
k = k + 1
End If
j = j + 1
Loop

strResults = Trim(strBuild)
MsgBox strResults
strResults = ""
strBuild = ""

Good Luck,
 
Back
Top