Sign in to follow this  
Followers 0
ripdad

StringTools

3 posts in this topic

#1 ·  Posted (edited)

Recently, I was formatting some web pages and scripted a few helpers.

While these functions are not perfect, they did help me alot.

Strings like: (i.e. something.) will cause Sentence2Array() to split more lines

because of it's periods.

Example in an array:

[1](i.

[2]e.

[3]something.)

Since it's rare a string might have it - I just did a manual edit to fix it.

Maybe later, I'll have a go at a better parser.

Sentence2Array() would have been nice with SRE's - but I had too many problems with it.

I'd overcome one problem and 2 more would show up. Perhaps a different strategy is in order.

For now, StringReplace() will do just fine.

The functions don't "inter-mingle". That is to say, they don't depend upon one another.

This is by design, so that they can be used separately, if needed.

You can always nest them, like in the last example of the script.

But of course, you can modify them to suit your needs, if you want that.

Anyway, I decided to release them to the forum. Perhaps they will be of use to someone.

I do have a few more functions that I can add. I need to run some more test with them, first.

As with any of the scripts I post -- they come with "no strings attached".

Comments or suggestions are welcome.

#include 'array.au3' ; <-- to show an example
;
; Examples
;
Local $s = '{  1   }  ,2  -  3   )  (  this  )is[  a  silly  string  ]to   test  with  ?)' & @CRLF & 'End of String.'
;
;Local $s = ClipGet()
;
$s = String_ProperSpacing($s)
MsgBox(0, 'String_ProperSpacing', $s)
;
MsgBox(0, 'Character_StripWS', Character_StripWS($s, '-!?', 1, 1, 4))
;
MsgBox(0, 'Character_Add', Character_Add($s, '-?', 1, 1, '*'))
;
MsgBox(0, 'String_Capitalize', String_Capitalize($s))
;
Local $a = Sentence2Array(String_ProperSpacing($s))
If IsArray($a) Then _ArrayDisplay($a, 'Sentence2Array')
;
;
; #FUNCTION# =======================================================================
; Name...........: String_Capitalize
; Description ...: Capitalizes the first letter (a thru z) found in a string.
; Syntax.........: $var = String_Capitalize($str)
; Parameters ....: $str = input string.
; Return values .: Returns processed string.
; Author .....:... ripdad
; Date Released .: February 05, 2012
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func String_Capitalize($str)
    Local $s = StringLeft(StringRegExpReplace($str, '(?i)[^a-z]', '1'), 1)
    If $s Then Return StringReplace($str, $s, StringUpper($s), 1)
    Return $str
EndFunc
;
; #FUNCTION# =======================================================================
; Name...........: String_Wrap
; Description ...: Wraps a string to the next available space from character number.
; Syntax.........: $var = String_Wrap($str, $num)
; Parameters ....: $str = input string.
;................: $num = number of characters to wrap at.
; Return values .: Success: Returns processed string.
;................: Sets @error -1 if empty string.
; Author ........: ripdad
; Date Released .: February 04, 2012
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func String_Wrap($str, $num)
    $str = StringRegExpReplace($str, '(.{' & $num & '}s)', '1' & @CRLF)
    If StringStripWS(StringLeft($str, 16), 8) Then Return $str
    Return SetError(-1, 0, 0)
EndFunc
;
; #FUNCTION# =======================================================================
; Name...........: Character_StripWS
; Description ...: Strips one or more spaces from specific character(s)
;................: (left, right or both sides)
; Syntax.........: $var = Character_StripWS($str, $char, $left, $right, $num)
; Parameters ....: $str = input string.
;................: $char = character(s) to remove space from.
;................: $left = left side of selected character; 0 = no, 1 = yes
;................: $right = right side of selected character; 0 = no, 1 = yes
;................: $num = number of spaces to remove from character.(default: all)
; Return values .: Success: Returns processed string.
;................: Sets @error -1 if empty string.
; Author ........: ripdad
; Date Released .: February 02, 2012
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func Character_StripWS($str, $char, $left, $right, $num = ''); $num = '' --> all spaces   $num = 1 --> one space
    If $right Then $str = StringRegExpReplace($str, '([' & $char & '])( {1,' & $num & '})', '1'); remove space(s) to right
    If $left Then $str = StringRegExpReplace($str, '( {1,' & $num & '})([' & $char & '])', '2'); remove space(s) to left
    If StringStripWS(StringLeft($str, 16), 8) Then Return $str
    Return SetError(-1, 0, 0)
EndFunc
;
; #FUNCTION# =======================================================================
; Name...........: Character_Add
; Description ...: Adds one or more characters to selected character(s)
;................: (left, right or both sides)
; Syntax.........: $var = Character_Add($str, $char, $left, $right, $add)
; Parameters ....: $str = input string.
;................: $char = character(s) to add to.
;................: $left = left side of selected character; 0 = no, 1 = yes
;................: $right = right side of selected character; 0 = no, 1 = yes
;................: $add = character(s) string to add.
; Return values .: Success: Returns processed string.
;................: Sets @error -1 if empty string.
; Author ........: ripdad
; Date Released .: February 02, 2012
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func Character_Add($str, $char, $left, $right, $add = ' '); default: one space
    If $right Then $str = StringRegExpReplace($str, '([' & $char & '])', '1' & $add); add character(s) to right
    If $left Then $str = StringRegExpReplace($str, '([' & $char & '])', $add & '1'); add character(s) to left
    If StringStripWS(StringLeft($str, 16), 8) Then Return $str
    Return SetError(-1, 0, 0)
EndFunc
;
; #FUNCTION# =======================================================================
; Name...........: String_ProperSpacing
; Description ...: Attempts to make right a normal word string.
; Syntax.........: $var = String_ProperSpacing($str)
; Parameters ....: $str = input string.
; Return values .: Success: Returns processed string.
;................: Sets @error -1 if empty string.
; Author ........: ripdad
; Date Released .: February 02, 2012
; Modified.......: February 04, 2012 - changed name of function
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func String_ProperSpacing($str)
    $str = StringReplace($str, @CRLF, '|')
    $str = StringStripWS($str, 7)
    $str = StringRegExpReplace($str, '([[({])', ' 1'); assure space to left
    $str = StringRegExpReplace($str, '([])}.,:;!?])', '1 '); assure space to right
    $str = StringRegExpReplace($str, '([[({|])( *)', '1'); remove spaces to right
    $str = StringRegExpReplace($str, '( *)([])}|.,:;!?])', '2'); remove spaces to left
    $str = StringReplace(StringReplace($str, ' [s]', '[s]'), ' (s)', '(s)')
    $str = StringStripWS($str, 7)
    $str = StringReplace($str, '|', @CRLF)
    If StringStripWS(StringLeft($str, 16), 8) Then Return $str
    Return SetError(-1, 0, 0)
EndFunc
;
; #FUNCTION# =======================================================================
; Name...........: Sentence2Array
; Description ...: Splits string into sentences that end in one of these  .!?
; Syntax.........: $var = Sentence2Array($str, $aggressive)
; Parameters ....: $str = input string.
;................: $aggressive; 0 = no, 1 = yes
; Return values .: Success: Returns an array of sentences.
;................: Sets @error -1 on fail
; Author ........: ripdad
; Date Released .: February 02, 2012
; Modified.......: February 04, 2012 - changed to SRE's
; Remarks .......: String must have a termination. ie: .!?
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/index.php
; Example .......: Yes
; ==================================================================================
Func Sentence2Array($str, $aggressive = 1)
    If Not StringRegExp($str, '[.!?]') Then Return SetError(-1, 0, 0)
    $str = StringReplace(StringReplace($str, @CR, ' '), @LF, ' ')
    Local $a
    If $aggressive Then
        $a = StringRegExp($str, 's*(.*?[.!?]s*[]}).!?"'']*)', 3)
    Else
        $a = StringRegExp($str, 's*(.*?[.!?]W*)', 3)
    EndIf
    $str = ''
    If Not IsArray($a) Then Return SetError(-1, 0, 0)
    For $i = 0 To UBound($a) - 1
        $str &= '|' & StringStripWS($a[$i], 3)
    Next
    Return StringSplit(StringTrimLeft($str, 1), '|')
EndFunc
;

Updates:

February 04, 2012 - A few minor changes and swapped out code in Sentence2Array() to SRE's.

Added String_Wrap().

February 06, 2012 - Added String_Capitalize(). Also changed the modified __ArrayDisplay() to the standard.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites



String2Array() is Very Similar to _StringExplode() and StringSplit(). I Think the Only thing that isnt Different is that Instead of Removing the Delimiter (Sentence Punctuation in this Case), it Leaves it. But Nice Job. :)

Formerly ReaperX

Share this post


Link to post
Share on other sites

ReaperX - Thanks for the comment.

Updated first post.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0