|
-
Aug 24th, 2006, 11:11 PM
#1
Thread Starter
Lively Member
[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?
Last edited by rami.haddad; Aug 25th, 2006 at 07:04 PM.
Reason: Resolved
-
Aug 24th, 2006, 11:30 PM
#2
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
-
Aug 24th, 2006, 11:39 PM
#3
Thread Starter
Lively Member
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
-
Aug 25th, 2006, 12:04 AM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 25th, 2006, 12:11 AM
#5
Thread Starter
Lively Member
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
-
Aug 25th, 2006, 12:14 AM
#6
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 25th, 2006, 12:47 AM
#7
Thread Starter
Lively Member
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
-
Aug 25th, 2006, 04:54 AM
#8
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))
-
Aug 25th, 2006, 05:22 AM
#9
Re: Parse a string in a special manner
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).
-
Aug 25th, 2006, 06:00 AM
#10
Re: Parse a string in a special manner
Or this:
VB Code:
MsgBox Format(Date,"Long Date")
-
Aug 25th, 2006, 03:45 PM
#11
Thread Starter
Lively Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|