Jump to content
Sign in to follow this  

[UDF] StringTrim

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.


- AutoIt v3.0.102 available below


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)


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


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:


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

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

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

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

     If $NewString = "" Then Return 2

     Return $NewString


#include "StringTrim.au3"

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

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

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


#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)

Edited by SlimShady

Share this post

Link to post
Share on other sites
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?



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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...