Skip to main content

字符串函数

字符串函数对文本数据执行操作。使用字符串函数清理数据、将数据转换为其它格式或大小写、对数据进行度量计算或执行其它操作。字符串函数只能与字符串 数据类型 一起使用。

Contains

Contains(String, Target, CaseInsensitive=1) :在一个字符串中搜索某一字符串的匹配项。如果字符串 (String) 包含目标 (Target),则返回 True,否则返回 False。

示例

Contains('123ABC', 'ABC') 返回 True。

Contains('123ABC', 'abc') 返回 True。

Contains('123ABC', 'abc', 0) 返回 False。

CountWords

CountWords(string) :返回指定字符串中的字数。字由空格分隔的字符定义。

示例

CountWords("Basic Variables Households") 返回 3。

CountWords("Basic Variables Age:Female (Pop)Age 1") 返回 5。

DecomposeUnicodeForMatch

DecomposeUnicodeForMatch(String) :转换为窄字符串时,删除重音符号并扩展复合字符。此函数接受 Alteryx Wstring 数据类型,并将其转换为小写的窄 字符串 数据类型。移除所有重音符号和其它修饰。有关 Alteryx 数据类型的详细信息,请参阅 数据类型

重要

此函数仅用于匹配,不被视为规范化字符串。

该函数不适用于非西方字符集(如日语等)。请避免对可能包含宽字符的字符串使用此函数,注意宽字符会转换为“?”。

示例

DecomposeUnicodeForMatch("Prénoms français") 返回 prenoms francais。

EndsWith

EndsWith(String, Target, CaseInsensitive=1) :检查字符串是否以特定的字符串结尾。如果 (String) 以 (Target) 结尾,则返回 True;否则,返回 False。默认行为不区分大小写。

示例

EndsWith('123ABC', 'ABC') 返回 True。

EndsWith('123ABC', 'abc') 返回 True。

EndsWith('123ABC', 'abc', 0) 返回 False。

FindNth

FindNth(Initial String, Target, Instance): Finds the instance (nth occurrence) of a target string in the initial string and returns the 0-indexed position of the instance. The function is case-sensitive and doesn’t accept negative parameters.

Example

FindNth("Hello World","o",2) returns 7. The function finds the 2nd instance of "o" in the provided string and returns the 0-indexed position, which in this case is 7.

FindNth("Hello World","World",1) returns 6. The function finds the 1st instance of "World" in the provided string and returns the 0-indexed position of the first character, which in this case is 6.

FindNth("Hello World","o",1.8) returns 7. The function finds the 2nd instance (rounds 1.8 to the nearest integer) of "o" in the provided string and returns the 0-indexed position of the 2nd character, which in this case is 7.

FindNth("Hello World","world",1) returns -1. The function finds the 1st instance of "world" in the provided string and returns the 0-indexed position of the first character, which in this case is -1 (match not found due to case sensitivity).

FindNth("Hello World","l",0) returns -1. The function finds the 0th instance of "l" in the provided string and returns the 0-indexed position, which in this case is -1 (not found because the 0th position doesn't exist).

FindNth("Hello World","l",-1) returns -1. Negative parameters are not applicable, so the function returns -1 (not found).

FindNth("Hello World", Null(), 1) returns -1. Null and empty are not found.

FindNth(Hello world, "l", 1) returns an error. The function requires that the first 2 parameters are strings.

FindString

重要

FindString 函数区分大小写。

FindString(String, Target) :搜索一个字符串 (String) 中特定字符串 (Target) 的匹配项,并返回特定字符串在字符串中的数字位置。

返回字符串 (String) 中首个目标 (Target) 的索引(从 0 开始)。如果不存在匹配项,则返回 -1。

示例

FindString([Name], "John") 在字符串以 John 开头时返回 0 ,否则返回 -1

IF (FINDSTRING([Name], "John") =0) THEN "John Smith" ELSE "Other" ENDIF 在字符串以 John 开头时返回 John Smith ,否则返回 Other

GetLeft

GetLeft(String, Delimiter) 返回首次出现指定的 1 个或多个分隔符 (Delimiter)字符串 (String) 的左侧部分。

分隔符运算符

请注意, 分隔符 运算符区分大小写。如果指定“a”作为分隔符,则该函数只能使用“a”作为分隔符,而不能使用“A”。

示例

GetLeft("Automated Analytics for All", " ") 返回“Automated”。

GetLeft("Enable fast, confident decisions across the enterprise.", ",") 返回“Enable fast”。

GetPart

GetPart(String, Delimiter, Index) :使用指定的 1 个或多个分隔符 (Delimiter) 将字符串 (String) 划分为多个子字符串,并返回索引 (Index) 中指定位置的子字符串。

分隔符运算符

请注意, 分隔符 运算符区分大小写。如果指定“a”作为分隔符,则该函数只能使用“a”作为分隔符,而不能使用“A”。

此函数使用从 0 开始的索引。例如,如果索引为 2,则函数从索引 2 的位置返回字符串,即返回第 3 个子字符串。

示例

GetPart("Automated Analytics for All", " ", 1) 返回“Analytics”。

GetPart("Automated Analytics for All", " ", 0) 返回 “Automated”。

GetPart("Enable fast, confident decisions across the enterprise.", ", ", 5) 返回“across”。请注意,此示例使用 2 个分隔符,即逗号和空格。该函数在遇到任一分隔符时拆分字符串。

GetPart("January 1, 2023", " ", 2) 返回“2023”。

GetRight

GetRight(String, Delimiter) : 返回字符串在指定的 1 个或多个(分隔符)首次出现的右侧部分。

分隔符运算符

请注意, 分隔符 运算符区分大小写。如果指定“a”作为分隔符,则该函数只能使用“a”作为分隔符,而不能使用“A”。

示例

GetRight("Automated Analytics for All", " ") 返回“Analytics for All”。

GetRight("Enable fast, confident decisions across the enterprise.", ",") 返回“ confident decisions across the enterprise”。(注意返回 confident 之前的空格)。

GetWord

GetWord(string, n) :返回字符串中的第 N 个(从 0 开始)单词(从 0 开始的索引表示第一个单词位于 0 位置,第二个单词位于 1 位置,依此类推)。单词指的是由空格、tab 键、回车符和换行字符分隔的字符集合。一行中的多个分隔符被视为一个分隔符。

示例

GetWord("Basic Variables Households", 0) 返回“Basic”。

GetWord("Basic Variables Households", 1) 返回“Variables”。

Left

Left(String, len) :返回字符串 (String) 中最左边长度为 (len) 的字符。如果 len 小于 0 或大于字符串 String 的长度,则字符串 String 保持不变。

示例

Left("92688", 3) 返回值“926”。

Length

Length(String) :返回字符串 (String) 的长度。

警告

此 Designer 函数和 SQL 函数的长度结果可能不同。这取决于任何特定数据库如何处理宽字符和换行符。例如,换行符在 PostgreSQL 中不计入长度,而在 Designer 长度函数中计入长度,于是 Designer 中的结果可能会有所不同,具体取决于使用的是 ANSI 还是 Unicode 驱动程序。

示例

Length("92688") 返回值 5。

LowerCase

LowerCase(String) :将字符串转换为小写。

示例

LowerCase("M1P 1G6") 返回"m1p 1g6"。

MD5_ASCII

MD5_ASCII(String) :计算字符串的 MD5 哈希值。字符串仅限为窄字符。在计算哈希值之前会将宽字符转换为“?”。用于字符串类型,不建议用于 WString 类型。您应该避免对可能包含宽字符的字符串使用此函数。

MD5_UNICODE

MD5_UNICODE(String) :在原始引擎或 AMP 中,计算存储为 UTF-16 的字符串的 MD5 哈希值。

MD5_UTF8

MD5_UTF8(String) :计算存储为 UTF-8 的字符串的 MD5 哈希值。

示例

Md5_Ascii("Lá") ,以 Latin1 存储并占用 2 个字节 4C E1。此函数计算字节的 MD5,结果为“0c0ee86cc87d87125ad8923562be952e”。

Md5_Ascii("Lá ☢") ,☢ 字符为宽字符并会被替换为“?”,因此就好像您在计算 Md5_Ascii("Lá?") 一样。它以 Latin1 存储并占用 3 个字节 4C E1 3F。此函数计算字节的 MD5,结果为“a5a308ab19acf900efea8fc7b5b77b4d”。

Md5_Unicode("Lá") ,以 UTF-16 存储并占用 4 个字节 4C 00 E1 00。此函数计算字节的 MD5,结果为“aa9969dfcca04249842cc457e5b3dd01”。

Md5_Unicode("Lá ☢") ,以 UTF-16 存储并占用 8 个字节 4C 00 E1 00 3C D8 C8 DF。此函数计算字节的 MD5,结果为“7c4762d93572dd02a8a405232e966b18”。

Md5_Utf8("Lá") ,以 UTF-8 存储并占用 3 个字节 4C C3 A1。此函数计算字节的 MD5,结果为“68f00289dc3be140b1dfd4e031d733f1”。

Md5_Utf8("Lá ☢") ,以 UTF-8 存储并占用 7 个字节 4C C3 A1 F0 9F 8F 88。此函数计算字节的 MD5,结果为“383fc0355db728a2078ce41a2ab6211b”。

PadLeft

PadLeft (String, len, char) :将指定字符向字符串的左边填充,直到字符串达到指定的长度。如果用于填充的字符 (char) 包含一个以上的字符,则仅使用第一个字符。

示例

PadLeft("M", 4, "x") 返回“xxxM”。

PadRight

PadRight (String, len, char) :将指定字符向字符串的右边填充,直到字符串达到指定的长度。如果用于填充的字符 (char) 包含一个以上的字符,则仅使用第一个字符。

示例

PadRight("M", 4, "x") 返回“Mxxx”。

REGEX_CountMatches

REGEX_CountMatches(String,pattern,icase) :返回字符串中与模式匹配的项计数。

icase 是可选参数。使用了此参数时,大小写必须匹配。默认情况下,icase=1 表示忽略大小写。如果设置为 0,则大小写必须匹配。

REGEX_Match

REGEX_Match(String,pattern,icase) :搜索字符串中正则表达式的匹配项。

判断字符串是否从第一个字符到最后一个字符与模式匹配。

  • 如果匹配不一定始于字符串之首,则在模式开头加上 “.*”。

  • 如果匹配项不一定在字符串之尾,则在模式结尾加上 “.*”。

icase 是可选参数。使用了此参数时,大小写必须匹配。默认情况下,icase=1 表示忽略大小写。如果设置为 0,则大小写必须匹配。

示例

REGEX_Match("123-45-6789", "\d{3}-\d{2}-\d{4}") 返回 -1 (True)。

REGEX_Replace

REGEX_Replace(String, pattern, replace, icase) :允许使用正则表达式替换文本,返回由正则表达式查找 (pattern) 和替换 (string) 生成的字符串。所有匹配项都将被替换,而不仅仅是第一项。

replace 参数可以是指定值(如下所示),也可以是一个标记的组(如“$1”)。确保标记的组用双引号括起来。

icase 是可选参数。使用了此参数时,大小写必须匹配。

  • 默认情况下 icase=1,表示忽略大小写。

  • 如果设置为 0,则大小写必须匹配。

示例

REGEX_Replace("Don't reveal your Social Security number, 123-45-6789","\d{3}-\d{2}-\d{4}", "CLASSIFIED") 返回“Don't reveal your Social Security number, CLASSIFIED”。

REGEX_Replace("Change all domain names from alteryx@Alteryx.com","@.*\.", "@extendthereach.") 返回“Change all domain names from alteryx@extendthereach.com”。

REGEX_Replace("25 test","(\d+)\s.*","$1") 返回 25。

Replace

Replace(String, Target, Replacement) :使用字符串 (Replacement) 替换出现的每一个字符串 (Target),再返回字符串 (String)。

示例

Replace("Good judgment comes from experience", "experience", "awareness") 返回“Good judgment comes from awareness”

替换字符

ReplaceChar(String, y, z) :使用字符 (z) 替换字符 (y) 的每个匹配项后返回字符串 (String)。如果替换字符 (z) 具有一个以上的字符,则只使用第一个字符。如果 (z) 为空,则每个与 (y) 中的任何一个字符匹配的字符 (String) 将被删除。

示例

ReplaceChar("abcdefb", "b", "_") 返回“a_cdef_”。

ReplaceChar("@a#b%c", "@,#,%", "_") 返回“_a_b_c”。

ReplaceFirst

ReplaceFirst(String, Target, Replacement) :使用字符串 (Replacement) 替换字符串 (Target) 的第一个匹配项后,返回字符串 (String)。

示例

ReplaceFirst("abcdefb", "b", "_") 返回“a_cdefb”。

ReverseString

ReverseString(String) :反转字符串中的所有字符。

示例

ReverseString("abcdefb") 返回“bfedcba”。

Right

Right(String, len) :返回字符串的最后 (len) 个字符。如果 len 小于 0 或大于 String 的长度,则字符串保持不变。

示例

Right("92688", 3) 返回值“688”。

StartsWith

StartsWith(String, Target, CaseInsensitive=1) :检查字符串是否以指定的字符串开头。如果字符串 String 以指定字符串 Target 开头,则返回 True,否则返回 False。

示例

StartsWith('ABC123', 'ABC') 返回 True。

StartsWith('ABC123', 'abc') 返回 True。

StartsWith('ABC123', 'abc', 0) 返回 False。

STRCSPN

STRCSPN(String, y) :返回字符串 (String) 开头连续的、 含字符串 (y) 中字符的字符串长度。

示例

STRCSPN("Bob's Amaco", "~!@#$%^&*'()") 返回 3。这对于测试字符串中有没有标点符号非常有用。

StripQuotes

StripQuotes(String) :删除字符串结尾的一组成对的引号或单引号。

示例

StripQuotes("Hello there") 返回“Hello there”。

StripQuotes("'Hello there,' she said.") 返回 'Hello there,' she said.

StripQuotes('"Hello there," she said.') 返回 "Hello there," she said.

STRSPN

STRSPN(String, y) :返回由字符串 (y) 中的字符组成的字符串 (String) 的初始段的长度。

示例

STRSPN("3034408896x105", "0123456789") 返回 10。这对于测试字符串是否包含一组指定字符非常有用。

Substring

Substring(String, start, length) :返回字符串 (String) 从 (start) 位置开始提取长度等于 (length) 的子字符串(如果提供了长度)。

示例

Substring("949-222-4356", 4, 8) 返回“222-4356”。

Substring("949-222-4356", 4, 6) 返回“222-43”。

Substring("949-222-4356", 4) 返回“222-4356”。

TitleCase

TitleCase(String) :将字符串转换为词首字母大写。

示例

TitleCase("john smith") 返回“John Smith”。

Trim

Trim(String, y) :删除字符串(String)首部和尾部出现的 y 中的任意字符。Y 是可选项,默认为去掉空白。提示:在 TRIM 函数示例中,指定的字符将被去掉。字符顺序不重要。

示例

Trim("!see instructions!!!", "!") 返回“see instructions”。

Trim(" Test123 ") 返回“Test123”。

TrimLeft

TrimLeft(String, y) :删除字符串 (String) 开头出现的字符串 (y) 中的字符。Y 是可选项,默认为去掉空白。

示例

TrimLeft("** special invitation ", " *") 返回“special invitation”。

TrimRight

TrimRight(String, y) :删除字符串 (String) 尾部出现的字符串 (y) 中的字符。Y 是可选项,默认为去掉空白。

当你输入要修剪的字符串时,TRIM 函数不遵守字符的顺序。它将字符串视为“列表”。因此,它将修剪列表中的所有字符。如果您要替换字符串,请在表达式中使用REPLACE 函数或 REGEX 函数。

示例

TrimRight("John Smith ") 返回 “John Smith”。

TrimRight("John Smith**","*") 返回 “John Smith”。

Uppercase

Uppercase(String) : 将字符串转换为大写。

示例

Uppercase("John Smith") 返回“JOHN SMITH”。

Uuid

UuidCreate() :创建唯一标识符。

示例

UuidCreate() 返回一个唯一值,如 ba131836-1ba3-4d42-8f7e-b81a99c2e838。

关于长度和字符串

当计算字符串 长度 时,第一个字符计为 1(字符串 “record” 的长度为6)。

但字符串中某一字符的位置是基于 之间有多少 字符。因此,应将第一个字符的位置视为 0。字符串 “record” 中字母 “c” 的位置则为 2。