Results 1 to 11 of 11

Thread: [RESOLVED] Parse a string in a special manner

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    113

    [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:
    1. dim d() as string
    2.  
    3. 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

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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:
    1. Dim i  As Integer
    2. Dim d() As String
    3.     d = Split("dddd, dd' de 'MMMM' de 'yyyy", "'")
    4.         For i = 0 To UBound(d)
    5.             If Left(d(i), 1) = " " Then d(i) = "'" & d(i) & "'"
    6.         Next i

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    113

    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:
    1. 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

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Parse a string in a special manner

    assuming that there is always some formatting characters before the ' (as your sample)

    VB Code:
    1. dim d() as string
    2.  
    3. d = split("dddd, dd' de 'MMMM' de 'yyyy","'")
    4. for i = 1 to ubound(d) step 2   ' each 2nd string was between single quotes
    5.      d(i) = "'" & d(i) & "'"
    6. 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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    113

    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

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    113

    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:
    1. Dim strLongDate As String
    2. Dim strResult As String
    3. Dim strSplitValues() As String
    4. Dim intCounter As Integer
    5.  
    6. Calendar = vbCalHijri
    7.  
    8. strLongDate = "dddd, dd' de 'MMMM' de 'yyyy"
    9.  
    10. strSplitValues = Split(strLongDate, "'")
    11.  
    12. For intCounter = 1 To UBound(strSplitValues) Step 2
    13.      strSplitValues(intCounter) = "'" & strSplitValues(intCounter) & "'"
    14. Next
    15.  
    16. strResult = ""
    17. For intCounter = 0 To UBound(strSplitValues)
    18.     If InStr(1, strSplitValues(intCounter), "'") = 0 Then
    19.         strResult = strResult & Format$(Date, strSplitValues(intCounter))
    20.     Else
    21.         strResult = strResult & Replace$(strSplitValues(intCounter), "'", "")
    22.     End If
    23. Next intCounter
    24.  
    25. MsgBox strResult

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Dim sParts() As String, N As Long, I As Long
    2.    
    3.     sParts = Split("dddd, dd' de 'MMMM' de 'yyyy", "'")
    4.     For N = 1 To UBound(sParts) Step 2
    5.         For I = 1 To Len(sParts(N)) * 2 Step 2
    6.             sParts(N) = Left$(sParts(N), I - 1) & "\" & Mid$(sParts(N), I)
    7.         Next I
    8.     Next N
    9.    
    10.     MsgBox Format$(Now, Join(sParts))

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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:
    1. MsgBox FormatDateTime(Date, vbLongDate)
    That will show the long date according to the locale's Long date format (Regional settings).

  10. #10
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Parse a string in a special manner

    Or this:

    VB Code:
    1. MsgBox Format(Date,"Long Date")
    Frans

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    113

    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
  •  



Click Here to Expand Forum to Full Width