This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
vba:functions:trims [25.08.2014 10:22:54] yaslaw [Beispiele] |
vba:functions:trims [11.07.2018 11:39:16] (current) yaslaw |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <const> | ||
+ | version=1.1.1 | ||
+ | vdate=11.07.2018 | ||
+ | fname=udf_trims.bas | ||
+ | ns=%NAMESPACE% | ||
+ | fpath=/vba/functions | ||
+ | </const> | ||
====== [VBA] trims() ====== | ====== [VBA] trims() ====== | ||
- | trims() ist eine Trim-Funktion für Strings. Im Gegensatz zu trim() entfernt trims() auch %%NewLines%% und Tabulatoren. Zudem kann auch ein RTrim oder LTrim ausgeführt werden | + | //trims() ist eine Trim-Funktion für Strings. Im Gegensatz zu trim() entfernt trims() auch %%NewLines%% und Tabulatoren. Zudem kann auch ein RTrim oder LTrim ausgeführt werden |
- | + | //==Version %%version%% - %%vdate%%== | |
- | {{:vba:functions:udf_trims.bas|Download udf_trims.bas}} | + | {{%%fname%%|Download %%fname%% (V-%%version%%)}} |
===== Definition ===== | ===== Definition ===== | ||
Line 31: | Line 38: | ||
===== Code ===== | ===== Code ===== | ||
- | <code vb udf_trims.bas>Attribute VB_Name = "udf_trims" | + | <source '%%fpath%%/%%fname%%' vb> |
- | '------------------------------------------------------------------------------- | + | |
- | 'File : udf_trims.bas | + | |
- | ' Copyright mpl by ERB software | + | |
- | ' All rights reserved | + | |
- | ' http://wiki.yaslaw.info/dokuwiki/doku.php/vba/ | + | |
- | 'Environment : VBA 2007 + | + | |
- | 'Version : 1.0.0 | + | |
- | 'Name : trims | + | |
- | 'Author : Stefan Erb (ERS) | + | |
- | 'History : 22.07.2014 - ERS - Creation | + | |
- | '------------------------------------------------------------------------------- | + | |
- | Option Explicit | + | |
- | + | ||
- | ' Entfernt alle Spaces vor und nach dem Text mittels RegExp. | + | |
- | ' Das s am Ende des Namens steht dann auch für \s. ALso alle Spaces | + | |
- | ' Die RegExp werden während ded ganzen Session gehalten um. Das beschleunigt die Funktion beim EInsatz in SQL | + | |
- | + | ||
- | Public Enum trDirection | + | |
- | trBoth = 0 'TRIM() | + | |
- | trLeft = 1 'LTRIM() | + | |
- | trRight = 2 'RTRIM() | + | |
- | End Enum | + | |
- | + | ||
- | Private Const C_PATTERN = "^\s*([\S\s]*\b)\s*$" | + | |
- | Private Const C_L_PATTERN = "^\s*([\S\s]*)$" | + | |
- | Private Const C_R_PATTERN = "^([\S\s]*\b)\s*$" | + | |
- | + | ||
- | 'Cache der RegExp | + | |
- | Private rxTrim(2) As Object | + | |
- | + | ||
- | '/** | + | |
- | ' * trim \s: Entfernt im Gegensatz zu trim() auch Zeilenumbrüche, Tabulatoren etc. Alles was regexp \s ist | + | |
- | ' * | + | |
- | ' * string = trims(string [,direction][,clearCache]) | + | |
- | ' * | + | |
- | ' * @param String | + | |
- | ' * @param trDirection Angabe, auf welcher Seite getrimmt werden soll | + | |
- | ' * @param Boolean Leehrt den Cache. Wird nur während der entwicklung gebraucht | + | |
- | ' * @return String | + | |
- | ' */ | + | |
- | Public Function trims(ByVal iString As String, Optional ByVal iDirection As trDirection = trBoth, Optional ByVal iReset As Boolean = False) As String | + | |
- | If rxTrim(iDirection) Is Nothing Or iReset Then | + | |
- | Set rxTrim(iDirection) = CreateObject("VBScript.RegExp") | + | |
- | rxTrim(iDirection).pattern = Choose(iDirection + 1, C_PATTERN, C_L_PATTERN, C_R_PATTERN) | + | |
- | End If | + | |
- | trims = rxTrim(iDirection).Replace(iString, "$1") | + | |
- | End Function</code> | + |