[RESOLVED] Parse a string in a special manner
Hello,
I would like to pasre this string in a special manner:
dddd, dd' de 'MMMM' de 'yyyy
The result I get by using the split function
VB Code:
dim d() as string
d = split("dddd, dd' de 'MMMM' de 'yyyy","'")
is this
dddd, dd
de
MMMM
de
yyyy
However what I want is this:
dddd, dd
' de '
MMMM
' de '
yyyy
Anybody have any ideas?
Re: Parse a string in a special manner
If format is always the same just re-do it a bit with a loop:
VB Code:
Dim i As Integer
Dim d() As String
d = Split("dddd, dd' de 'MMMM' de 'yyyy", "'")
For i = 0 To UBound(d)
If Left(d(i), 1) = " " Then d(i) = "'" & d(i) & "'"
Next i
Re: Parse a string in a special manner
Thanks gavio, you have helped me on several occasions, I do appreciate it.
The format is not always the same.
I am using GetLocaleInfo to determine the locale's Long date format, which returns as an exmple for Spain:
"dddd, dd' de 'MMMM' de 'yyyy"
What the single quotes mean is that anything between the single-quote should not be formated.
So if I do this
VB Code:
Format$(date,dddd, dd' de 'MMMM' de 'yyyy)
The result should return
Monday 24 de August de 2006
So I need to parse and know which items are between single quotes
Re: Parse a string in a special manner
assuming that there is always some formatting characters before the ' (as your sample)
VB Code:
dim d() as string
d = split("dddd, dd' de 'MMMM' de 'yyyy","'")
for i = 1 to ubound(d) step 2 ' each 2nd string was between single quotes
d(i) = "'" & d(i) & "'"
next
Re: Parse a string in a special manner
Thanks for the help, but the quotes can be anywhere and not necessarly with a space in between. However I am assuming that the quotes come in pairs with the GetLocaleInfo function
I am using GetLocaleInfo to determine the locale's Long date format, which returns as an exmple for Spain:
"dddd, dd' de 'MMMM' de 'yyyy"
What the single quotes mean is that anything between the single-quote should not be formated.
So if I do this
visual basic code:
Format$(date,dddd, dd' de 'MMMM' de 'yyyy)
The result should return
Monday 24 de August de 2006
So I need to parse and know which items are between single quotes
Re: Parse a string in a special manner
the code i posted before will work correctly as long as the single quote is not at the very beginning or end of the string, if the unformatted is at the beginning or end it will not be right, but otherwise it will do what you want, no matter how many or the contents of the literal string
Re: Parse a string in a special manner
Thank you westcon1 for your assistance, you are correct it works. I have cleaned up the code. Here is what I have which is working:
VB Code:
Dim strLongDate As String
Dim strResult As String
Dim strSplitValues() As String
Dim intCounter As Integer
Calendar = vbCalHijri
strLongDate = "dddd, dd' de 'MMMM' de 'yyyy"
strSplitValues = Split(strLongDate, "'")
For intCounter = 1 To UBound(strSplitValues) Step 2
strSplitValues(intCounter) = "'" & strSplitValues(intCounter) & "'"
Next
strResult = ""
For intCounter = 0 To UBound(strSplitValues)
If InStr(1, strSplitValues(intCounter), "'") = 0 Then
strResult = strResult & Format$(Date, strSplitValues(intCounter))
Else
strResult = strResult & Replace$(strSplitValues(intCounter), "'", "")
End If
Next intCounter
MsgBox strResult
Re: Parse a string in a special manner
if this is a date format then all you have to do it make sure the bits between the single quotes are displayed properly - by putting a "\" in front of each character.:
VB Code:
Dim sParts() As String, N As Long, I As Long
sParts = Split("dddd, dd' de 'MMMM' de 'yyyy", "'")
For N = 1 To UBound(sParts) Step 2
For I = 1 To Len(sParts(N)) * 2 Step 2
sParts(N) = Left$(sParts(N), I - 1) & "\" & Mid$(sParts(N), I)
Next I
Next N
MsgBox Format$(Now, Join(sParts))
Re: Parse a string in a special manner
Quote:
I am using GetLocaleInfo to determine the locale's Long date format, which returns as an exmple for Spain:
"dddd, dd' de 'MMMM' de 'yyyy"
What the single quotes mean is that anything between the single-quote should not be formated.
Instead of doing all that (including the use of GetLocaleInfo), why dont you use..
VB Code:
MsgBox FormatDateTime(Date, vbLongDate)
That will show the long date according to the locale's Long date format (Regional settings).
Re: Parse a string in a special manner
Or this:
VB Code:
MsgBox Format(Date,"Long Date")
Re: Parse a string in a special manner
Thanks to westconn1 and bshmobile. It would seem bshmobile's solution works even when the single quote is at the beginning or the end. However I have already implemented westconns solution for this release of the software. Next release I will try bshmobile's. Thanks again for your assistance
-----------------------
Reply to jcis and Fran C
The reason I am not using MsgBox FormatDateTime(Date, vbLongDate)
is because I need to supply my own month name and date in the required format and have to parse the string myself.