Jump to content

[UDF] StringTrim


SlimShady
 Share

Recommended Posts

First: I didn't know this was so easy to make :D

This is a function that trims a substring by specifying:

  • the string
  • From which character to start...
  • ... until character(s) ...
If you know that the string contains a substring multiple times, you can set which occurence to start from.

If it doesn't match anything, it will return 1.

If the result is an empty string, it will return 2.

Requirements:

- AutoIt v3.0.102 available below

http://www.autoitscript.com/autoit3/files/unstable/autoit/

Changes since previous version:

- Using the last argument you can tell the function whether you're gonna specify numbers or a string of characters

(Examples at the bottom)

Information

StringTrim ( "string", "substring 1" , "substring 2" , occurance 1, occurance 2, "type" )

Parameters:

string The string to evaluate.

substring 1 The start of the substring to trim (can be a character string or a number)

substring 2 The end of the substring to trim ^^^^

If you specify -1 as the value it will trim everything until the end of the string.

occurance 1 Which occurance of substring 1 to find in the string.

occurance 2 Which occurance of substring 2 to find in the string.

"type" Type: "char" or "num"

Here's the code:

#include-once 

Func StringTrim($String, $Start, $End, $StartOccur, $EndOccur, $Type)
     Local $num = 0
     If $Type = "char" Then
         $Match1 = StringInStr($String, $Start, 0, $StartOccur)
         $Match2 = StringInStr($String, $End, 0, $EndOccur)
         If $Match1 = 0 Then Return 1
     EndIf

     If $Type = "num" Then
         $Match1 = $Start
         $Match2 = $End
     EndIf

         If $Match2 < $Match1 Then
            Do
                $num = $num + 1
                $Match2 = StringInStr($String, $End, 0, $num)
                If $Match2 = 0 Then Return 1
                If @error = 1 Then Return 1
            Until $Match2 > $Match1
         EndIf

     If $End = -1 Then
        $NewString = StringLeft($String, $Match1 - 1)
     Else
        $StringLeft = StringLeft($String, $Match1 - 1)
        $StringRight = StringRight($String, StringLen($String) - $Match2)
        $NewString = $StringLeft & $StringRight
     EndIf

     If $NewString = "" Then Return 2

     Return $NewString
EndFunc

Examples:

#include "StringTrim.au3"

$String = "[..This is an [..Trim this, please..] example sentence..]"

$NewString = StringTrim($String, "[", "]", 2, 1, "char")

MsgBox(0, "New String", $NewString)

Exit

#include "StringTrim.au3"

$String = "[..This is an [..Trim this, please..] example sentence..]"

$Match1 = StringInStr($String, "[", 0, 2)
$Match2 = StringInStr($String, "]", 0, 1)

$NewString = StringTrim($String, $Match1, $Match2, 1, 1, "num")

MsgBox(0, "New String", $NewString)

Exit
Edited by SlimShady
Link to comment
Share on other sites

  • 3 weeks later...
Guest cobra2411

I tried to use the example that you posted, but I recieved an error. The function StringInStr won't take the occurence argument.

Am I missing something?

Thanks,

David

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...