• Print

Author Topic: String Manipulation  (Read 994 times)

dustinian

  • Newbie
  • *
  • Posts: 19
    • dustinian.com
    • Email
String Manipulation
« on: April 17, 2012, 11:13:14 AM »
Below is the [string.manipulation.bi] library that I initally worked on for Text Transformation Language. Immediately after this topic, I will post a reply with a [.bas] file that demonstrates this library.

UPDATE 5/28/2012: I changed the code below to version 1.6, which includes various fixes and updates. More judicious use of INSTR functions cut runtime by approximately 30% (measured by running functions on large e-books such as "Moby Dick").

All advice is welcomed!

Code: [Select]
'---------------------------------------------------------------------------------------------------
'STRING MANIPULATION
'---------------------------------------------------------------------------------------------------
'SUMMARY:
'   Purpose:  A library of custom functions that transform strings.
'   Author:   Dustinian Camburides (dustinian@gmail.com)
'   Platform: QB64 (www.qb64.net)
'   Revision: 1.6
'   Updated:  5/28/2012
'---------------------------------------------------------------------------------------------------
'REVISION HISTORY
'   1.6: Amend: Used pointers (rather than INSTR functions) in WHILE statements (increased efficiency).
'               Removed procedure-level [strText] variable (reduced memory footprint).
'               Removed "IF" statements from pointer calculations (increased efficiency).
'   1.5: Fixed: Made all pointers (Location, Start, Length, etc.) LONG instead of INTEGER for strings greater than 32kb.
'   1.4: Amend: [Replace$] renamed to [Replace_Once$], and [Replace_All$] renamed to [Replace$] for clarity.
'   1.3: Fixed: Infinite loop in [Replace_From$] when [Add] sub-string was zero-length.
'               Infinite loop in [Replace_Between$] when [Add] sub-string was zero-length.
'               Infinite loop in [Replace_Subsequent$] when [Add] sub-string was zero-length.
'               Logic error in [Replace_If_Between$] where only the first [Find] was replaced.
'   1.2: Added: Detailed comments on the purpose, inputs, and outputs added to each function.
'   1.1: Added: [Start] parameter for the [Between$] function.
'               [Start] parameter for the [Before$] function.
'               [Start] parameter for the [After$] function.
'               [Start] parameter for the [Replace_Once$] function.
'        Fixed: Runtime error in [Replace_From$] function when [Precedant] or [Antecedant] did not appear.
'               Runtime error in [Replace_Between$] function when [Precedant] or [Antecedant] did not appear.
'               Runtime error in [Replace_Before_Next$] function when [Precedant] or [Antecedant] did not appear.
'               Runtime error in [Replace_Subsequent$] function when [Precedant] or [Antecedant] did not appear.
'               Runtime error in [Replace_If_Between$] function when [Precedant] or [Antecedant] did not appear.
'               Runtime error in [Between$] function when [Precedant] or [Antecedant] did not appear.
'               Invalid output from [Before$] function when [Find] did not appear.
'               Invalid output from [After$] function when [Find] did not appear.
'               Invalid output from [Between$] function when [Find] did not appear.
'               Invalid output from [Replace_Once$] function when [Find] did not appear.
'   1.0: First working version.
'---------------------------------------------------------------------------------------------------
'PLANNED ENHNACEMENTS
'   Minor:
'       - Add scope control (ByVal, ByRef) to each parameter in each function.
'       - Add scope control (Public, Private) to each function.
'   Major:
'       - Make the [Start] parameter optional for [Replace_Once$] function.
'       - Make the [Start] parameter optional for [Between$] function.
'       - Make the [Start] parameter optional for [Before$] function.
'       - Make the [Start] parameter optional for [After$] function.
'---------------------------------------------------------------------------------------------------
'PROCEDURES
'   - FUNCTION Replace_From$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Add AS STRING)
'   - FUNCTION Replace_Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Add AS STRING)
'   - FUNCTION Replace_Subsequent$ (Text AS STRING, Precedant AS STRING, Find AS STRING, Add AS STRING)
'   - FUNCTION Replace_If_Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Find AS STRING, Add AS STRING)
'   - FUNCTION Replace$ (Text AS STRING, Find AS STRING, Add AS STRING)
'   - FUNCTION Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Start as INTEGER)
'   - FUNCTION Before$ (Text AS STRING, Find AS STRING, Start AS INTEGER)
'   - FUNCTION After$ (Text AS STRING, Find AS STRING, Start AS INTEGER)
'   - FUNCTION Filter$ (Text AS STRING, Allowed AS STRING)
'---------------------------------------------------------------------------------------------------
FUNCTION Replace_From$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Add AS STRING)'
    'SUMMARY:
       '[Replace_From$] replaces characters in the [Text] string from the start of the [Precedant] sub-string to the end of the [Antecedant] sub-string with the [Add] sub-string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Precedant: The sub-string that denotes the start of the change within [Text].
        'Antecedant: The sub-string that denotes the end of the change within [Text].
        'Add: The sub-string that's being added to the [Text] string.
    'VARIABLES:
        DIM lngLocation AS LONG     'The address of the [Find] substring within the [Text] string.
        DIM lngStart AS LONG        'The location of the [Precedant] sub-string within the [Text] string.
        DIM lngStop AS LONG         'The location of the [Antecedant] sub-string within the [Text] string.
    'INITIALIZE:
        'Locate the [Precedant]:
            lngStart = INSTR(1, Text, Precedant)
    'PROCESSING:
        'While the [Precedant] appears in the [Text]...
            WHILE lngStart
                'Locate the [Antecedant]:
                    lngStop = INSTR((lngStart + LEN(Precedant)), Text, Antecedant)
                'If the [Antecedant] appears in the [Text]...
                    IF lngStop THEN
                        'Locate the last character of the [Antecedant]:
                            lngStop = lngStop + LEN(Antecedant) - 1
                        'Replace the text:
                            Text = LEFT$(Text, (lngStart - 1)) + Add + RIGHT$(Text, (LEN(Text) - lngStop))
                    ELSE
                        'Increment the start point:
                            lngStart = lngStart + INSTR((lngStart + 1), Text, Precedant)
                    END IF
                'Increment the start point:
                    lngStart = INSTR((lngStart + LEN(Precedant) + LEN(Add) + LEN(Antecedant)), Text, Precedant)
        'Next instance of [Precedant]...
            WEND
    'OUTPUT:
        Replace_From$ = Text
END FUNCTION

FUNCTION Replace_Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Add AS STRING)
    'SUMMARY:
       '[Replace_From$] replaces characters in the [Text] string from the end of the [Precedant] sub-string to the start of the [Antecedant] sub-string with the [Add] sub-string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Precedant: The sub-string that denotes the start of the change within [Text].
        'Antecedant: The sub-string that denotes the end of the change within [Text].
        'Add: The sub-string that's being added to the [Text] string.
    'VARIABLES:
        DIM lngLocation AS LONG     'The address of the [Find] substring within the [Text] string.
        DIM lngStart AS LONG        'The location of the [Precedant] sub-string within the [Text] string.
        DIM lngStop AS LONG         'The location of the [Antecedant] sub-string within the [Text] string.
    'INITIALIZE:
        'Locate the [Precedant]:
            lngStart = INSTR(1, Text, Precedant)
    'PROCESSING:
        'While the [Precedant] appears in the [Text]...
            WHILE lngStart
                'Locate the [Antecedant] in the [Text]...
                    lngStop = INSTR((lngStart + LEN(Precedant)), Text, Antecedant)
                'If the [Antecedant] appears in the [Text]...
                    IF lngStop THEN
                        'Locate the last character of the [Antecedant]:
                            lngStop = lngStop - 1
                        'Replace the text:
                            Text = LEFT$(Text, (lngStart + LEN(Precedant) - 1)) + Add + RIGHT$(Text, (LEN(Text) - lngStop))
                    ELSE
                        'Increment the start point:
                            lngStart = INSTR((lngStart + 1), Text, Precedant)
                    END IF
            'Increment the start point:
                lngStart = INSTR((lngStart + LEN(Precedant) + LEN(Add) + LEN(Antecedant)), Text, Precedant)
        'Next instance of [Precedant]...
            WEND
    'OUTPUT:
        Replace_Between$ = Text
END FUNCTION

FUNCTION Replace_Subsequent$ (Text AS STRING, Precedant AS STRING, Find AS STRING, Add AS STRING)
    'SUMMARY:
       '[Replace_Subsequent$] replaces the first [Find] sub-string after the [Precedant] sub-string with the [Add] sub-string within the [Text] string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Precedant: The sub-string that denotes the start of the change within [Text].
        'Find: The specified sub-string; the string sought within the [Text] string.
        'Add: The sub-string that's being added to the [Text] string.
    'VARIABLES:
        DIM lngStart AS LONG        'The location of the [Precedant] sub-string within the [Text] string.
        DIM lngStop AS LONG         'The location of the last character of the [Find] sub-string within the [Text] string.
        DIM lngLocation AS LONG     'The address of the [Find] substring within the [Text] string.
    'INITIALIZE:
        lngOldStart = 0
        lngStart = INSTR(1, Text, Precedant)
    'PROCESSING:
        'While the [Precedant] appears in the [Text]...
            WHILE lngStart
                'Locate the [Find] sub-string:
                    lngLocation = INSTR((lngStart + LEN(Precedant)), Text, Find)
                'If [Find] appears in the [Text]...
                    IF intLocation THEN
                        'Locate the last character of [Find]:
                            lngStop = intLocation + LEN(Find) - 1
                        'Replace the text:
                            Text = LEFT$(Text, (lngLocation - 1)) + Add + RIGHT$(Text, (LEN(Text) - lngStop))
                        'Increment the start point:
                            lngStart = INSTR((lngStart + LEN(Precedant) + LEN(Add)), Text, Precedant)
                    ELSE
                        'Increment the start point:
                            lngStart = INSTR((lngStart + 1), Text, Precedant)
                    END IF
        'Next instance of [Precedant]...
            WEND
    'OUTPUT:
        Replace_Subsequent$ = Text
END FUNCTION

FUNCTION Replace_If_Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Find AS STRING, Add AS STRING)
    'SUMMARY:
       '[Replace_If_Between$] Replaces all instances of the [Find] sub-string that appear between the [Precedant] and [Antecedant] sub-strings with the [Add] sub-string in the [Text] string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Precedant: The sub-string that denotes the start of the change within [Text].
        'Antecedant: The sub-string that denotes the end of the change within [Text].
        'Find: The specified sub-string; the string sought within the [Text] string.
        'Add: The sub-string that's being added to the [Text] string.
    'VARIABLES:
        DIM lngStart AS LONG        'The location of the [Precedant] sub-string within the [Text] string.
        DIM lngLocation AS LONG     'The address of the [Find] substring within the [Text] string.
        DIM strSubString AS STRING  'The text between the [Precedant] and [Antecedant] sub-strings within the [Text] string.
    'INITIALIZE:
        'Locate the [Precedant] string within the [Text] string:
            lngStart = INSTR(1, Text, Precedant)
    'PROCESSING:
        'While the [Precedant] appears in the [Text]...
            WHILE lngStart
                'Locate the [Antecedant]:
                    lngLocation = INSTR((lngStart + LEN(Precedant)), Text, Antecedant)
                'If the [Antecedant] appears in the [Text]...
                    IF lngLocation THEN
                        'Extract the substring:
                            strSubString = MID$(Text, (lngStart + LEN(Precedant)), (lngLocation - (lngStart + LEN(Precedant))))
                        'Replace the text within the substring:
                            strSubString = Replace$(strSubString, Find, Add)
                        'Insert the new substring into the text:
                            Text = LEFT$(Text, ((lngStart + LEN(Precedant)) - 1)) + strSubString + RIGHT$(Text, (LEN(Text) + 1 - lngLocation))
                    ELSE
                        'Increment the start point:
                            lngStart = INSTR(lngLocation, Text, Precedant)
                    END IF
                'Increment the start point:
                    lngStart = INSTR(lngStart + LEN(Precedant) + LEN(strSubString) + LEN(Antecedant), Text, Precedant)
        'Next instance of [Precedant]...
            WEND
    'OUTPUT:
        Replace_If_Between$ = Text
END FUNCTION

FUNCTION Replace$ (Text AS STRING, Find AS STRING, Add AS STRING)
    'SUMMARY:
       '[Replace$] replaces all instances of the [Find] sub-string with the [Add] sub-string within the [Text] string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Find: The specified sub-string; the string sought within the [Text] string.
        'Add: The sub-string that's being added to the [Text] string.
    'VARIABLES:
        DIM lngLocation AS LONG 'The address of the [Find] substring within the [Text] string.
        DIM strBefore AS STRING 'The characters before the string to be replaced.
        DIM strAfter AS STRING  'The characters after the string to be replaced.
    'INITIALIZE:
            lngLocation = INSTR(1, Text, Find)
    'PROCESSING:
        'While [Find] appears in [Text]...
            WHILE lngLocation
                'Extract all text before the [Find] substring:
                    strBefore = LEFT$(Text, lngLocation - 1)
                'Extract all text after the [Find] substring:
                    strAfter = RIGHT$(Text, ((LEN(Text) - (lngLocation + LEN(Find) - 1))))
                'Return the substring:
                    Text = strBefore + Add + strAfter
                'Locate the next instance of [Find]:
                    lngLocation = INSTR(1, Text, Find)
        'Next instance of [Find]...
            WEND
    'OUTPUT:
        Replace$ = Text
END FUNCTION

FUNCTION Between$ (Text AS STRING, Precedant AS STRING, Antecedant AS STRING, Start AS LONG)
    'SUMMARY:
       '[Between$] returns all the characters from the [Text] string that appear between the [Precedant] and [Antecedant] sub-strings.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Precedant: The sub-string that denotes the start of the change within [Text].
        'Antecedant: The sub-string that denotes the end of the change within [Text].
        'Start: The first location within the [Text] string where the [Find] string is sought.
    'VARIABLES:
        DIM lngLocation AS LONG  'The address of the [Find] substring within the [Text] string.
        DIM lngStart AS LONG     'The address of the end of the [Precedant].
        DIM lngStop AS LONG      'The address of the beginning of the [Antecedant].
        DIM lngLength AS LONG    'The length of the sub-string to be extracted.
    'PROCESSING:
        'Locate the [Precedant]:
            lngLocation = INSTR(Start, Text, Precedant)
        'If the [Precedant] string was present...
            IF lngLocation THEN
                'Move the pointer ot the end of the [Precedant]:
                    lngStart = lngLocation + LEN(Precedant)
                'Locate the [Antecedant]:
                    lngLocation = INSTR(lngStart + 1, Text, Antecedant)
                'If the [Antecedant] was present...
                    IF lngLocation THEN
                        'Calculate the characters to keep:
                            lngLength = (lngLocation - lngStart)
                        'Return the substring:
                            Between$ = MID$(Text, lngStart, lngLength)
                    ELSE
                        'Return a null string:
                            Between$ = ""
                    END IF
            ELSE
                'Return a null string:
                    Between$ = ""
            END IF
END FUNCTION

FUNCTION Before$ (Text AS STRING, Find AS STRING, Start AS LONG)
    'SUMMARY:
       '[Before$] returns all the characters from the [Text] string that appear before the [Find] sub-string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Find: The specified sub-string; the string sought within the [Text] string.
        'Start: The first location within the [Text] string where the [Find] string is sought.
    'VARIABLES:
        DIM lngLocation AS LONG  'The address of the [Find] substring within the [Text] string.
        DIM lngLength AS LONG    'The length of the sub-string to be extracted.
    'PROCESSING:
        'Locate the [Find] string within the [Text] string:
            lngLocation = INSTR(Start, Text, Find)
        'If the [Find] string was present...
            IF lngLocation THEN
                'Calculate the characters to keep:
                    lngLength = lngLocation - 1
                'Return the substring:
                    Before$ = LEFT$(Text, lngLength)
            ELSE
                'Return a null string:
                    Before$ = ""
            END IF
END FUNCTION

FUNCTION After$ (Text AS STRING, Find AS STRING, Start AS LONG)
    'SUMMARY:
       '[After$] returns all the characters from the [Text] string that appear after the [Find] sub-string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Find: The specified sub-string; the string sought within the [Text] string.
        'Start: The first location within the [Text] string where the [Find] string is sought.
    'VARIABLES:
        DIM lngLocation AS LONG     'The address of the [Find] substring within the [Text] string.
        DIM lngLength AS LONG       'The length of the [Output] substring.
    'PROCESSING:
        'Locate the [Find] string within the [Text] string:
            lngLocation = INSTR(Start, Text, Find)
        'If the [Find] string was present...
            IF lngLocation THEN
                'Calculate the characters to keep:
                    lngLength = Len(Text) - (lngLocation + LEN(Find) - 1)
                'Return the substring:
                    After$ = RIGHT$(Text, lngLength)
            ELSE
                'Return a null string:
                    After$ = ""
            END IF
END FUNCTION

FUNCTION Filter$ (Text AS STRING, Allowed AS STRING)
    'SUMMARY:
       '[Filter$] removes all characters from the [Text] string that do not appear in the [Allowed] string.
    'INPUT:
        'Text: The input string; the text that's being manipulated.
        'Allowed: The characters allowed in the output.
    'VARIABLES:
        DIM strTemporary AS STRING      'The [Temporary] string that holds the [Output] string as it is assembled.
        DIM strCharacter AS STRING * 1  'The [Character] substring extracted from the [Text] string.
        DIM lngCharacter AS LONG        'The address of the current [Character] in the [Text] string.
    'INITIALIZE:
        strTemporary = ""
    'PROCESSING:
        'For each [Character]...
            FOR lngCharacter = 1 TO LEN(Text)
                'Extract the character:
                    strCharacter = MID$(Text, lngCharacter, 1)
                'If the character appears in the [Allowed] string...
                    IF INSTR(1, Allowed, strCharacter) THEN
                        'Add [Character] to the [Temporary] string:
                            strTemporary = strTemporary + strCharacter
                    END IF
        'Next [Character]...
            NEXT lngCharacter
    'OUTPUT:
        Filter$ = strTemporary
END FUNCTION
« Last Edit: May 28, 2012, 12:57:47 PM by dustinian »
Dustinian Camburides

dustinian

  • Newbie
  • *
  • Posts: 19
    • dustinian.com
    • Email
String Manipulation Demo
« Reply #1 on: April 17, 2012, 11:15:11 AM »
Below is the "demo" [.bas] file. Compile with the [string.manipulation.bi] library and execute!

UPDATE (5/28/2012): Modified the [.bas] below to work with version 1.6 of the [.bi] above.

Code: [Select]
'---------------------------------------------------------------------------------------------------
'STRING MANIPULATION DEMO
'---------------------------------------------------------------------------------------------------
'SUMMARY
'   Purpose:  Demonstrates the functions in [string.manipulation.bi].
'   Author:   Dustinian Camburides
'   Platform: QB64 (www.qb64.net)
'   Revision: 1.1
'   Updated:  5/28/2012
'---------------------------------------------------------------------------------------------------
'INSTRUCTIONS
'   1. Add this .BAS file and [string.manipulation.bi] to your QB64 folder.
'   2. Compile this .BAS file.
'   3. Run the compiled file.
'---------------------------------------------------------------------------------------------------
'REVISION HISTORY
'   1.1: Amend: Updated to v1.6 of [string.manipulation.bi].
'   1.0: First working version.
'---------------------------------------------------------------------------------------------------
'PLANNED ENHNACEMENTS
'   Minor:
'       - None at this time.
'   Major:
'       - None at this time.
'---------------------------------------------------------------------------------------------------
'PROCEDURES
'   - SUB New_Screen()
'       - SUB Print_Header()
'       - SUB Print_Before()
'       - SUB Print_Function_Menu
'   - FUNCTION Prompt_User%()
'   - SUB Demo_Function(Choice AS INTEGER, Text AS STRING)
'       - SUB Demo_Replace_From(Text AS STRING)
'       - SUB Demo_Replace_Between(Text AS STRING)
'       - SUB Demo_Replace_Subsequent(Text AS STRING)
'       - SUB Demo_Replace_If_Between(Text AS STRING)
'       - SUB Demo_Replace(Text AS STRING)
'       - SUB Demo_Between(Text AS STRING)
'       - SUB Demo_Before(Text AS STRING)
'       - SUB Demo_After(Text AS STRING)
'       - SUB Demo_Filter(Text AS STRING)
'   - SUB Print_Quit()
'---------------------------------------------------------------------------------------------------
'CONSTANTS:
    CONST SAMPLETEXT = "     Yet Peter Blood, who was not only able to bear arms, but trained and skilled in their use, who was certainly no coward, and a papist only when it suited him, tended his geraniums and smoked his pipe on that warm July evening as indifferently as if nothing were afoot. One other thing he did. He flung after those war-fevered enthusiasts a line of Horace--a poet for whose work he had early conceived an inordinate affection: 'Quo, quo, scelesti, ruitis?'"
'VARIABLES:
    DIM intChoice AS INTEGER
    DIM strText AS STRING
'PROCESSING:
    'Print a New Screen:
        Call New_Screen
    'Print Menu:
        Call Print_Function_Menu
    'Initialize Loop:
        intChoice = Prompt_User
    'Loop input:
        WHILE intChoice <> 0
            'Prompt User:
            'Validate input
                IF intChoice > 0 AND intChoice < 10 THEN
                    Call Demo_Function(intChoice, SAMPLETEXT)
                ELSE
                    COLOR 4
                    PRINT "Error: Please enter a number between 0 and 9."
                END IF
            'Prompt the user:
                intChoice = Prompt_User
        WEND
    'Print 'Quit' message:
        Call Print_Quit
END

SUB New_Screen()
    'Clear the screen:
        CLS
    'Print Header:
        Call Print_Header
    'Print Before:
        Call Print_Before
END SUB

SUB Print_Header()
    COLOR 15
    PRINT "STRING MANIPULATION DEMO"
    PRINT
END SUB

SUB Print_Before()
    COLOR 15
    PRINT "Before:"
    COLOR 07
    PRINT SAMPLETEXT
    PRINT
END SUB

SUB Print_Function_Menu
    COLOR 15
    PRINT "Function:"
    COLOR 4
    PRINT "      0: QUIT THIS DEMO"
    COLOR 3
    PRINT "      1: Replace_From$"
    PRINT "      2: Replace_Between$"
    PRINT "      3: Replace_Subsequent$"
    PRINT "      4: Replace_If_Between$"
    PRINT "      5: Replace_All$"
    PRINT "      6: Between$"
    PRINT "      7: Before$"
    PRINT "      8: After$"
    PRINT "      9: Filter$"
END SUB

FUNCTION Prompt_User%()
    DIM intChoice AS INTEGER
    COLOR 11
    INPUT "     Enter a number..."; intChoice
    Prompt_User% = intChoice
END FUNCTION

SUB Demo_Function(Choice AS INTEGER, Text AS STRING)
    'Print a New Screen:
        Call New_Screen
    'Demo the Function:
        SELECT CASE Choice
            CASE 1
                Call Demo_Replace_From(Text)
            CASE 2
                Call Demo_Replace_Between(Text)
            CASE 3
                Call Demo_Replace_Subsequent(Text)
            CASE 4
                Call Demo_Replace_If_Between(Text)
            CASE 5
                Call Demo_Replace(Text)
            CASE 6
                Call Demo_Between(Text)
            CASE 7
                Call Demo_Before(Text)
            CASE 8
                Call Demo_After(Text)
            CASE 9
                Call Demo_Filter(Text)
        END SELECT
    'Pause the Demo
        Sleep
    'Restore the New Screen:
        Call New_Screen
    'Resotre the prompt:
        Call Print_Function_Menu
END SUB

SUB Demo_Replace_From(Text AS STRING)
    COLOR 15
    PRINT "Replace From:"
    COLOR 3
    PRINT "     SYNTAX:  Replace_From$ (Text AS STRING,"
    PRINT "                             Precedant AS STRING,"
    PRINT "                             Antecedant AS STRING,"
    PRINT "                             Add AS STRING)"
    PRINT "     EXAMPLE: Replace_From$ (SAMPLE_TEXT, " + CHR$(34) + "Yet" + CHR$(34) + ", " + CHR$(34) + ". " + CHR$(34) + ", " + CHR$(34) + "" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Replaces everything from 'Yet' to . with nothing"
    PRINT
    COLOR 15
    PRINT "Result:"
    COLOR 14
    PRINT Replace_From$(Text, "Yet", ". ", "")
END SUB

SUB Demo_Replace_Between(Text AS STRING)
    COLOR 15
    PRINT "Replace Between:"
    COLOR 3
    PRINT "     SYNTAX:  Replace_Between$ (Text AS STRING,"
    PRINT "                                Precedant AS STRING,"
    PRINT "                                Antecedant AS STRING,"
    PRINT "                                Add AS STRING)"
    PRINT "     EXAMPLE: Replace_Between$ (SAMPLE_TEXT, " + CHR$(34) + "Yet" + CHR$(34) + ", " + CHR$(34) + ". " + CHR$(34) + ", " + CHR$(34) + "" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Replaces everything between 'Yet' and . with nothing"
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    PRINT Replace_Between$(Text, "Yet", ". ", "")
END SUB

SUB Demo_Replace_Subsequent(Text AS STRING)
    COLOR 15
    PRINT "Replace Subsequent:"
    COLOR 3
    PRINT "     SYNTAX:  Replace_Subsequent$ (Text AS STRING,"
    PRINT "                                   Precedant AS STRING,"
    PRINT "                                   Find AS STRING,"
    PRINT "                                   Add AS STRING)"
    PRINT "     EXAMPLE: Replace_Subsequent$ (SAMPLE_TEXT, " + CHR$(34) + "Yet" + CHR$(34) + ", " + CHR$(34) + "," + CHR$(34) + ", " + CHR$(34) + ";" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Replaces first , after 'Yet'"
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    PRINT Replace_Subsequent$(Text, "Yet", ",", ";")
END SUB

SUB Demo_Replace_If_Between(Text AS STRING)
    COLOR 15
    PRINT "Replace_If_Between:"
    COLOR 3
    PRINT "     SYNTAX:  Replace_If_Between$ (Text AS STRING,"
    PRINT "                                   Precedant AS STRING,"
    PRINT "                                   Antecedant AS STRING,"
    PRINT "                                   Find AS STRING,"
    PRINT "                                   Add AS STRING)"
    PRINT "     EXAMPLE: Replace_If_Between$ (SAMPLE_TEXT, " + CHR$(34) + "'" + CHR$(34) + ", " + CHR$(34) + "'" + CHR$(34) + ", " + CHR$(34) + "," + CHR$(34) + ", " + CHR$(34) + ";" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Replaces , with ; if between ' and '"
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    PRINT Replace_If_Between$(Text, "'", "'", ",", ";")
END SUB

SUB Demo_Replace(Text AS STRING)
    COLOR 15
    PRINT "Replace:"
    COLOR 3
    PRINT "     SYNTAX:  Replace$ (Text AS STRING,"
    PRINT "                        Find AS STRING,"
    PRINT "                        Add AS STRING)"
    PRINT "     EXAMPLE: Replace$ (SAMPLE_TEXT, " + CHR$(34) + "." + CHR$(34) + ", " + CHR$(34) + "!" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Replaces . with !"
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    PRINT Replace$(Text, ".", "!")
END SUB

SUB Demo_Between(Text AS STRING)
    COLOR 15
    PRINT "Between:"
    COLOR 3
    PRINT "     SYNTAX:  Between$ (Text AS STRING,"
    PRINT "                        Precedant AS STRING,"
    PRINT "                        Antecedant AS STRING,"
    PRINT "                        Start as INTEGER)"
    PRINT "     EXAMPLE: Between$ (SAMPLE_TEXT, " + CHR$(34) + "," + CHR$(34) + ", " + CHR$(34) + "," + CHR$(34) + ", 1)"
    COLOR 1
    PRINT "              Note: Returns between the first commas."
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    Print Between$(Text, ",", ",", 1)
END SUB

SUB Demo_Before(Text AS STRING)
    COLOR 15
    PRINT "Before:"
    COLOR 3
    PRINT "     SYNTAX:  Before$ (Text AS STRING,"
    PRINT "                       Find AS STRING,"
    PRINT "                       Start AS INTEGER)"
    PRINT "     EXAMPLE: Before$ (SAMPLE_TEXT, " + CHR$(34) + "." + CHR$(34) + ", 1)"
    COLOR 1
    PRINT "              Note: Returns all before the first period."
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    Print Before$(Text, ".", 1)
END SUB

SUB Demo_After(Text AS STRING)
    COLOR 15
    PRINT "After:"
    COLOR 3
    PRINT "     SYNTAX:  After$ (Text AS STRING,"
    PRINT "                      Find AS STRING,"
    PRINT "                      Start AS INTEGER)"
    PRINT "     EXAMPLE: After$ (SAMPLE_TEXT, " + CHR$(34) + "." + CHR$(34) + ", 1)"
    COLOR 1
    PRINT "              Note: Returns all after the first period."
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    Print After$(Text, ".", 1)
END SUB

SUB Demo_Filter(Text AS STRING)
    COLOR 15
    PRINT "Filter:"
    COLOR 3
    PRINT "     SYNTAX:  Filter$ (Text AS STRING,"
    PRINT "                       Allowed AS STRING)"
    PRINT "     EXAMPLE: Filter$ (Text AS STRING, " + CHR$(34) + "-.,?'" + CHR$(34) + ")"
    COLOR 1
    PRINT "              Note: Keeps punctuation only."
    PRINT
    COLOR 15
    PRINT "     Result:"
    COLOR 14
    Print Filter$(Text, "-.,?'")
END SUB

SUB Print_Quit()
    CLS
    COLOR 12
    PRINT
    PRINT "DEMO of string.manipulation.bi"
    PRINT
    PRINT "Thank you for using this demonstration!"
    PRINT
    PRINT "Excerpt used from 'Captain Blood' by Rafael Sabatini (1922, public domain)."
END SUB

'$INCLUDE: 'string.manipulation.bi'
« Last Edit: May 28, 2012, 01:01:09 PM by dustinian »
Dustinian Camburides

LeChuck

  • Hero Member
  • *****
  • Posts: 895
  • 18 * 37
Re: String Manipulation
« Reply #2 on: April 17, 2012, 12:41:16 PM »
Hey dustinian,

I tested this and it works great, are you planning to use this for a text game?
Two or more, use a FOR!

dustinian

  • Newbie
  • *
  • Posts: 19
    • dustinian.com
    • Email
Re: String Manipulation
« Reply #3 on: April 18, 2012, 03:21:29 AM »
Quote
I tested this and it works great, are you planning to use this for a text game? -LeChuck

Yes, but in a roundabout way.

I actually wrote this library to support "Text Transformation Language," which I posted in the main discussion forum some time ago. It's a utility. It's a scripting language for transforming text files. I have used that to convert website content into XML that can be imported into a CMS, and I've used it to move websites from one CMS format to another.

So it's actually meant to be a "utility." In fact, I can't see much use for the [string.manipulation.bi] library in games, since--even in a text-based adventure--your text should be pre-formatted. Could parts of this be used to clean up the user's input? Sure. I wrote another little set of routines specifically for that (QBFilter, posted in the Network 54 forums) some time ago.

So why is it yes in a roundabout way?

Well, the next place I'm going is using the [Between$] function and a few other functions from [string.manipulation.bi] to create an XML parser/reader... and then I'd like to use the XML parser/reader to create a "branching" game (a "Choose Your Own Adventure").

But that project is sort of on the back-burner. I'm more interested in how QB64 can be put to work, especially since so many other folks are putting it to play. (And no slight against them!)
Dustinian Camburides

dustinian

  • Newbie
  • *
  • Posts: 19
    • dustinian.com
    • Email
Re: String Manipulation
« Reply #4 on: May 28, 2012, 01:06:09 PM »
[string.manipulation.bi] has been updated to version 1.6, which includes various fixes and enhancements (detailed in the "revision history" of the code).

Please let me know if you find any issues.

My next project is a series of revisions to Text Transformation Language.

If anyone knows of a project involving many text transformations on a large number of files (for example, moving a hard-coded website into XML format for import into WordPress or any other CMS, or moving old html4 code into html5 compliance), let me know, as I would like a test case.
Dustinian Camburides

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: String Manipulation
« Reply #5 on: July 16, 2012, 10:20:04 AM »
ooooooooooooooooooooooooops! i was merely letting people know i have an algo that's tested and can be used as part of any library or program they want. oh well whatever, nevermid. sincerely, the ghost of kurt. ;D
« Last Edit: July 16, 2012, 10:53:08 AM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

Clippy

  • Hero Member
  • *****
  • Posts: 16440
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: String Manipulation
« Reply #6 on: July 16, 2012, 10:45:25 AM »
This forum is for finished DECLARE LIBRARY DLL Libraries. Not regular QB64 code. Suggest you move to Samples or Discussion Forums.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

dustinian

  • Newbie
  • *
  • Posts: 19
    • dustinian.com
    • Email
Re: String Manipulation
« Reply #7 on: July 16, 2012, 04:36:06 PM »
*shrug* tell you what, go ahead and delete it.

if this community isn't interested in qb64 coders coding in qb64, i'll switch to FB.

thanks, all.
Dustinian Camburides

Clippy

  • Hero Member
  • *****
  • Posts: 16440
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: String Manipulation
« Reply #8 on: July 16, 2012, 05:40:56 PM »
Please don't scare me like that... ;)
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

Pete

  • Moderator
  • Hero Member
  • *****
  • Posts: 6240
  • Cuz I sez so varmint!
Re: String Manipulation
« Reply #9 on: July 16, 2012, 07:04:53 PM »
'm fine with the code as written staying right where it is. I'm also big on test manipulation, and it's nice to see a few other people skilled in that aspect of programming contribute to the forum.

Pete
It's only rocket science; it's not Linux!

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
Re: String Manipulation
« Reply #10 on: July 16, 2012, 08:17:22 PM »
If QB64 finished libraries shouldn't go here, then I'd love to see a place dedicated for sharing them.  Most people don't bother to compile routines into a .bi or .bm library UNLESS they are something they consider either essential to expand QB64 capabilities (like Terry's GLineInput), or a very common process they don't want to retype over and over.  (I have a whole collection of those.)

Sample often seem to me to be a place to post "concept programs", or "works in progress.". Discussion seems to be the place to go to ask questions, get a kickstart of advice, and sort out a specific problem in the code.  The Library area seems to me, that it's more of the place to go once everything is finished, compiled, and set to just plug-in and use.

Personally, I'd love to see more finished QB64 Libraries, just to see what other people think are essential or common processes, and want to share their solution.

Eventually, this is going to be a dead area on the boards, I imagine.  QB64 grows more powerful all the time, and is needing to rely less and less on dynamic libraries.  QB64GL is going to be even more stand-alone, and independent...   There's less and less need to rely on a DLL to get things done, when a little creative use can do the same job with a .BI and .BM
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

TerryRitchie

  • Hero Member
  • *****
  • Posts: 2264
  • FORMAT C:\ /Q /U /AUTOTEST (How to repair Win8)
    • Email
Re: String Manipulation
« Reply #11 on: July 16, 2012, 08:57:24 PM »
Quote from: Clippy on July 16, 2012, 10:45:25 AM
This forum is for finished DECLARE LIBRARY DLL Libraries. Not regular QB64 code. Suggest you move to Samples or Discussion Forums.

For cripe's sake Clippy, let the moderators or Galleon make these decisions on what is posted in the appropriate spots.

You saying that all the work I have done on my libraries should be moved to the samples section because they are not DECLARE LIBRARY?

You can be a real pain in the ASCII you know that?!  I'm ticked because good forum members like Dustinian don't need to be driven away over petty nonsense like this.

You live in Virginia Beach, it's the height of summer, you're retired, get away from the keyboard for a while and live life a bit.

Clippy

  • Hero Member
  • *****
  • Posts: 16440
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: String Manipulation
« Reply #12 on: July 16, 2012, 10:01:35 PM »
It SAYS LIBRARIES! IT SAYS FINISHED LIBRARIES! WTF Do what you want! No wonder nobody can find anything! Just BURY EACH OTHERS CODE FOR ALL I CARE!

MODERATOR MY ASS! WHO WANTED RULES HERE? Well you don't I guess.
« Last Edit: July 17, 2012, 09:29:01 AM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

  • Print