Extracting Words Function using a Delimiter Character 
Author Message
 Extracting Words Function using a Delimiter Character

Greetings...

Does anyone know of a Visual Basic function, or has coded a function
that will extract a word from a string, that has the ability to use a
Delimiter Character?

Or perhaps they know how to remove the 'month' portion of a 'date'?
I'm looking at having a variable which is the type: date.  To which, I
want to be able to extract the 'day' portion, 'month' and 'year'
portions.

The ultimate purpose is I want to determine the difference in days,
months, and years of two dates.

But to get back to my search for an extracting function...

Such as:
If anyone is familiar with TinyMush, there is a function called
Extract(), where given a list of words, we can do something like this:

    Syntax:
    Extract(List of words,starting position,number of words,[delimiter
character])

    Examples:
    Extract("This is a sentence.", 2, 2) = "is a"
    Where it would extract the second word and pull out the next word,
where the delimiter character is by default a space.

    Extract("This is a sentence.", 1,1) = "This"

But, as I stated before, I'm looking for something that handles
delimiter characters...

    Extract("This|is|a|sentence",1,2,|) = "This|is"

If anyone knows of a function that will allow me to do this...or even
better, a function that allows me to extract the day/month/year of a
date...or even...is able to determine the difference (and be able to
handle leap years) between two dates...I would love to hear from you...

Or perhaps we can discuss how to code this 'Extract()' function of
mine...

Cajun Nights Mush            />       Who Wants to Live Forever?
Cajun.Targonia.com 7373     /<                ICQ:13682963

[\\\\\\\\\\(O):::<======================================-

BourbonStreet/1373/          \>               John "Imagicka"
Blackthorne.



Wed, 18 Jun 1902 08:00:00 GMT  
 Extracting Words Function using a Delimiter Character
Hi John

Does this do what you want?

Function GetYMD(StartDate As String, _
    Optional EndDate As String = "", _
    Optional FutureDateText As String = " remaining.") As String

'*********************************************************
'*                                                       *
'* Parameters:                                           *
'*    StartDate - required                               *
'*                                                       *
'*    EndDate - Optional, defaults to the current date   *
'*                                                       *
'* Returns:                                              *
'*                                                       *
'*    The Years, Months & Days as a formatted string     *
'*                                                       *
'*********************************************************

Dim Years As Integer
Dim Months As Integer
Dim Days As Integer
Dim IsFuture As Boolean
If Not IsDate(StartDate) Then
    GetYMD = "Start date is invalid"
    Exit Function
End If
If Len(EndDate) = 0 Then
    EndDate = Date
Else
    If Not IsDate(EndDate) Then
        GetYMD = "End date is invalid"
        Exit Function
    End If
End If
If CDate(StartDate) > CDate(EndDate) Then
    Dim tmpDate As String
    tmpDate = EndDate
    EndDate = StartDate
    StartDate = tmpDate
    IsFuture = True
End If
Years = Year(EndDate) - Year(StartDate)
Months = Month(EndDate) - Month(StartDate)
Days = Day(EndDate) - Day(StartDate)
If Days < 0 Then
    Months = Months - 1
    Days = Day(DateSerial(Year(StartDate), Month(StartDate) + 1, 0)) + Days
End If
If Months < 0 Then
    Years = Years - 1
    Months = 12 + Months
End If
GetYMD = IIf(Years = 0, "", _
    IIf(Years = 1, Years & " year ", Years & " years ")) _
    & IIf(Months = 0, "", _
    IIf(Months = 1, Months & " month ", Months & " months ")) _
    & IIf(Days = 0, "", _
    IIf(Days = 1, Days & " day", Days & " days")) _
    & IIf(IsFuture, FutureDateText, "")
End Function

Or maybe one of these

Function GetDHMSFromDates(StartDateTime As Date, _
                EndDateTime As Date, _
                Optional ShowDays As Boolean = True, _
                Optional ReturnAsString As Boolean = True) As Variant
Dim tStr As String
Dim Retval(1 To 4) As Long
tStr = Format(DateDiff("s", StartDateTime, EndDateTime) / 86400, _
        "dd mm yyyy hh:nn:ss")
If ShowDays Then
    Retval(1) = DateDiff("d", #12:00:00 AM#, tStr)
    Retval(2) = Hour(tStr)
Else
    Retval(2) = DateDiff("h", #12:00:00 AM#, tStr)
End If
Retval(3) = Minute(tStr)
Retval(4) = Second(tStr)
If ReturnAsString Then
    If ShowDays Then
        tStr = Retval(1) & " days " & Retval(2) _
        & " hours " & Retval(3) & " minutes " & Retval(4) & " seconds"
    Else
        tStr = Retval(2) & " hours " & Retval(3) & " minutes " _
        & Retval(4) & " seconds"
    End If
    GetDHMSFromDates = tStr
Else
    GetDHMSFromDates = Retval()
End If
End Function

Function GetDHMSFromNumber(NumMinSec As Double, _
                Optional AsSeconds As Boolean = True, _
                Optional ShowDays As Boolean = True, _
                Optional ReturnAsString As Boolean = True) As Variant
Dim tStr As String
Dim dblDate As Double
Dim Retval(1 To 4) As Long
If AsSeconds Then
    dblDate = NumMinSec / 86400
Else
    dblDate = NumMinSec / 1440
End If
tStr = Format(dblDate, _
        "dd mm yyyy hh:nn:ss")
If ShowDays Then
    Retval(1) = DateDiff("d", #12:00:00 AM#, tStr)
    Retval(2) = Hour(tStr)
Else
    Retval(2) = DateDiff("h", #12:00:00 AM#, tStr)
End If
Retval(3) = Minute(tStr)
Retval(4) = Second(tStr)
If ReturnAsString Then
    If ShowDays Then
        tStr = Retval(1) & " days " & Retval(2) _
        & " hours " & Retval(3) & " minutes " & Retval(4) & " seconds"
    Else
        tStr = Retval(2) & " hours " & Retval(3) & " minutes " _
        & Retval(4) & " seconds"
    End If
    GetDHMSFromNumber = tStr
Else
    GetDHMSFromNumber = Retval()
End If
End Function

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> Greetings...

> Does anyone know of a Visual Basic function, or has coded a function
> that will extract a word from a string, that has the ability to use a
> Delimiter Character?

> Or perhaps they know how to remove the 'month' portion of a 'date'?
> I'm looking at having a variable which is the type: date.  To which, I
> want to be able to extract the 'day' portion, 'month' and 'year'
> portions.

> The ultimate purpose is I want to determine the difference in days,
> months, and years of two dates.

> But to get back to my search for an extracting function...

> Such as:
> If anyone is familiar with TinyMush, there is a function called
> Extract(), where given a list of words, we can do something like this:

>     Syntax:
>     Extract(List of words,starting position,number of words,[delimiter
> character])

>     Examples:
>     Extract("This is a sentence.", 2, 2) = "is a"
>     Where it would extract the second word and pull out the next word,
> where the delimiter character is by default a space.

>     Extract("This is a sentence.", 1,1) = "This"

> But, as I stated before, I'm looking for something that handles
> delimiter characters...

>     Extract("This|is|a|sentence",1,2,|) = "This|is"

> If anyone knows of a function that will allow me to do this...or even
> better, a function that allows me to extract the day/month/year of a
> date...or even...is able to determine the difference (and be able to
> handle leap years) between two dates...I would love to hear from you...

> Or perhaps we can discuss how to code this 'Extract()' function of
> mine...

> Cajun Nights Mush            />       Who Wants to Live Forever?
> Cajun.Targonia.com 7373     /<                ICQ:13682963

> [\\\\\\\\\\(O):::<======================================-

> BourbonStreet/1373/          \>               John "Imagicka"
> Blackthorne.



Wed, 18 Jun 1902 08:00:00 GMT  
 Extracting Words Function using a Delimiter Character


Quote:
> Does anyone know of a Visual Basic function, or has coded a function
> that will extract a word from a string, that has the ability to use a
> Delimiter Character?

Look at Instr, InstrRev, Left$, Mid$, Right$, Split, Join and StrReverse

Quote:
> Or perhaps they know how to remove the 'month' portion of a 'date'?
> I'm looking at having a variable which is the type: date.  To which, I
> want to be able to extract the 'day' portion, 'month' and 'year'
> portions.

Look at the Day, Month and Year functioncd

Quote:
> The ultimate purpose is I want to determine the difference in days,
> months, and years of two dates.

Look at DateDiff and DateAdd

<cut>

--
Please reply via the newsgroup only

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Extracting Words Function using a Delimiter Character
John,

Here's a function that does exactly what you are describing in the
beginning of your message.
Function Scan(Text_Str, n, delim As String)
'   Module: Scan
'   Function: Returns the nth word from a string of characters.  The words
'               are separated by a character.
'
'   Author: Joseph Jandal
'   Initial:02/09/94
'   Parms:
'       1.  Text_Str: an input parameter holding a line of text
'       2.  n: an input paramter holding the relative position of the word
in the text
'       3.  the delimiter
'   Mods:
'       Date    Comments
'--------------------------------------------------------------------------
---
    'This function returns the nth non-blank word from a string of chars.

    Dim words, Tlen, delpos, WordLen As Integer
    Dim Word, F_Str As String
    On Error GoTo ErrScan
    Scan = ""
    F_Str = Trim$(Text_Str)              'trim leading blanks
    Tlen = Len(F_Str)
    If n <= 0 Or Tlen = 0 Then Exit Function
    For words = 1 To n
        Scan = ""
        If Tlen = 0 Then Exit Function
        delpos = InStr(1, F_Str, delim)     'Find next space.
        If delpos > 0 Then
            Word = Left(F_Str, delpos - 1)        'Return word
        Else
            Word = F_Str        'Return word
        End If
        Scan = Word
        If words = n Then
            'MsgBox F_Str & " Scan returned word" & n & "=" & Word
            Exit Function
        End If
        'If words = 18 Then MsgBox "Before LTRIM String=" & F_Str
        If delpos > 0 Then
            F_Str = Mid(F_Str, delpos + 1)
            Else: F_Str = ""
        End If
        Tlen = Len(F_Str)
    Next words
    Exit Function
ErrScan:
    Debug.Print Error(Err)
End Function

Quote:

> Greetings...

> Does anyone know of a Visual Basic function, or has coded a function
> that will extract a word from a string, that has the ability to use a
> Delimiter Character?

> Or perhaps they know how to remove the 'month' portion of a 'date'?
> I'm looking at having a variable which is the type: date.  To which, I
> want to be able to extract the 'day' portion, 'month' and 'year'
> portions.

> The ultimate purpose is I want to determine the difference in days,
> months, and years of two dates.

> But to get back to my search for an extracting function...

> Such as:
> If anyone is familiar with TinyMush, there is a function called
> Extract(), where given a list of words, we can do something like this:

>     Syntax:
>     Extract(List of words,starting position,number of words,[delimiter
> character])

>     Examples:
>     Extract("This is a sentence.", 2, 2) = "is a"
>     Where it would extract the second word and pull out the next word,
> where the delimiter character is by default a space.

>     Extract("This is a sentence.", 1,1) = "This"

> But, as I stated before, I'm looking for something that handles
> delimiter characters...

>     Extract("This|is|a|sentence",1,2,|) = "This|is"

> If anyone knows of a function that will allow me to do this...or even
> better, a function that allows me to extract the day/month/year of a
> date...or even...is able to determine the difference (and be able to
> handle leap years) between two dates...I would love to hear from you...

> Or perhaps we can discuss how to code this 'Extract()' function of
> mine...

> Cajun Nights Mush            />       Who Wants to Live Forever?
> Cajun.Targonia.com 7373     /<                ICQ:13682963

> [\\\\\\\\\\(O):::<======================================-

> BourbonStreet/1373/          \>               John "Imagicka"
> Blackthorne.

--
Posted via CNET Help.com
http://www.help.com/


Wed, 18 Jun 1902 08:00:00 GMT  
 Extracting Words Function using a Delimiter Character
The Split function literally splits a string into an array of strings and
you can specify whatever delimiter you want--space is the default.
So,

Dim s as String
Dim sPart as string
Dim sHold() as String

s="VB is cool"
sHold() = Split(s, " ")
sPart = sHold(LBound(sHold))

sPart, then, holds "VB"

Try it out. It is really a cool new function--new to VB 6.

Hope this helps,

John


Quote:
> John,

> Here's a function that does exactly what you are describing in the
> beginning of your message.
> Function Scan(Text_Str, n, delim As String)
> '   Module: Scan
> '   Function: Returns the nth word from a string of characters.  The words
> '               are separated by a character.
> '
> '   Author: Joseph Jandal
> '   Initial:02/09/94
> '   Parms:
> '       1.  Text_Str: an input parameter holding a line of text
> '       2.  n: an input paramter holding the relative position of the word
> in the text
> '       3.  the delimiter
> '   Mods:
> '       Date    Comments

'--------------------------------------------------------------------------

- Show quoted text -

Quote:
> ---
>     'This function returns the nth non-blank word from a string of chars.

>     Dim words, Tlen, delpos, WordLen As Integer
>     Dim Word, F_Str As String
>     On Error GoTo ErrScan
>     Scan = ""
>     F_Str = Trim$(Text_Str)              'trim leading blanks
>     Tlen = Len(F_Str)
>     If n <= 0 Or Tlen = 0 Then Exit Function
>     For words = 1 To n
>         Scan = ""
>         If Tlen = 0 Then Exit Function
>         delpos = InStr(1, F_Str, delim)     'Find next space.
>         If delpos > 0 Then
>             Word = Left(F_Str, delpos - 1)        'Return word
>         Else
>             Word = F_Str        'Return word
>         End If
>         Scan = Word
>         If words = n Then
>             'MsgBox F_Str & " Scan returned word" & n & "=" & Word
>             Exit Function
>         End If
>         'If words = 18 Then MsgBox "Before LTRIM String=" & F_Str
>         If delpos > 0 Then
>             F_Str = Mid(F_Str, delpos + 1)
>             Else: F_Str = ""
>         End If
>         Tlen = Len(F_Str)
>     Next words
>     Exit Function
> ErrScan:
>     Debug.Print Error(Err)
> End Function


> > Greetings...

> > Does anyone know of a Visual Basic function, or has coded a function
> > that will extract a word from a string, that has the ability to use a
> > Delimiter Character?

> > Or perhaps they know how to remove the 'month' portion of a 'date'?
> > I'm looking at having a variable which is the type: date.  To which, I
> > want to be able to extract the 'day' portion, 'month' and 'year'
> > portions.

> > The ultimate purpose is I want to determine the difference in days,
> > months, and years of two dates.

> > But to get back to my search for an extracting function...

> > Such as:
> > If anyone is familiar with TinyMush, there is a function called
> > Extract(), where given a list of words, we can do something like this:

> >     Syntax:
> >     Extract(List of words,starting position,number of words,[delimiter
> > character])

> >     Examples:
> >     Extract("This is a sentence.", 2, 2) = "is a"
> >     Where it would extract the second word and pull out the next word,
> > where the delimiter character is by default a space.

> >     Extract("This is a sentence.", 1,1) = "This"

> > But, as I stated before, I'm looking for something that handles
> > delimiter characters...

> >     Extract("This|is|a|sentence",1,2,|) = "This|is"

> > If anyone knows of a function that will allow me to do this...or even
> > better, a function that allows me to extract the day/month/year of a
> > date...or even...is able to determine the difference (and be able to
> > handle leap years) between two dates...I would love to hear from you...

> > Or perhaps we can discuss how to code this 'Extract()' function of
> > mine...

> > Cajun Nights Mush            />       Who Wants to Live Forever?
> > Cajun.Targonia.com 7373     /<                ICQ:13682963

> > [\\\\\\\\\\(O):::<======================================-

> > BourbonStreet/1373/          \>               John "Imagicka"
> > Blackthorne.

> --
> Posted via CNET Help.com
> http://www.help.com/



Sat, 29 Mar 2003 13:19:33 GMT  
 Extracting Words Function using a Delimiter Character
What version of vb are you using?
if its vb6 you can use split:

function extract(words as string, st as integer, nwords as integer, delm as
string) as string
dim s
s = split(words,delm)
extract = ""
for cnt = 0 to nwords - 1
  if cnt=0 then
    extract = s(cnt)
  else
    extract = extract & delm & s(cnt)
  end if
next
end function

in prior versions:
function extract(words as string, st as integer, nwords as integer, delm as
string) as string
cnt = 0
res=1
do while cnt<nwords and res >0 and res < len(words)
res = instr(res + 1,words,delm)
loop
extract = left(words,res)
end function

These should work although I didn't test them.
they are pretty ugly though

Richard Kitchen


Quote:
> Greetings...

> Does anyone know of a Visual Basic function, or has coded a function
> that will extract a word from a string, that has the ability to use a
> Delimiter Character?

> Or perhaps they know how to remove the 'month' portion of a 'date'?
> I'm looking at having a variable which is the type: date.  To which, I
> want to be able to extract the 'day' portion, 'month' and 'year'
> portions.

> The ultimate purpose is I want to determine the difference in days,
> months, and years of two dates.

> But to get back to my search for an extracting function...

> Such as:
> If anyone is familiar with TinyMush, there is a function called
> Extract(), where given a list of words, we can do something like this:

>     Syntax:
>     Extract(List of words,starting position,number of words,[delimiter
> character])

>     Examples:
>     Extract("This is a sentence.", 2, 2) = "is a"
>     Where it would extract the second word and pull out the next word,
> where the delimiter character is by default a space.

>     Extract("This is a sentence.", 1,1) = "This"

> But, as I stated before, I'm looking for something that handles
> delimiter characters...

>     Extract("This|is|a|sentence",1,2,|) = "This|is"

> If anyone knows of a function that will allow me to do this...or even
> better, a function that allows me to extract the day/month/year of a
> date...or even...is able to determine the difference (and be able to
> handle leap years) between two dates...I would love to hear from you...

> Or perhaps we can discuss how to code this 'Extract()' function of
> mine...

> Cajun Nights Mush            />       Who Wants to Live Forever?
> Cajun.Targonia.com 7373     /<                ICQ:13682963

> [\\\\\\\\\\(O):::<======================================-

> BourbonStreet/1373/          \>               John "Imagicka"
> Blackthorne.



Fri, 04 Apr 2003 12:07:49 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Query Expressions to extract text from a record whilst recognising a text seperator/delimiter

2. Extracting images to file from Word using VB automation

3. Unable to extract resources from DLL's using API functions

4. Extracting Word Doc Titles using VB....code examples

5. Strings longer than 255 characters into word field using vb6

6. read chinese character words from XML file using filectl

7. Q: Replacing delimiter with CRLF (string function?)

8. Extracting Characters in Access 2000

9. Extract valid characters

10. Delimiter ignored when using TransferText method?

11. Delimiter ignored when using TransferText method?

12. using tab as delimiter in split()

 

 
Powered by phpBB® Forum Software