字串函式 string-functions

字串函式可讓您操控和處理歷程運算式中的文字值。 這些功能對於客戶歷程中的文書處理、驗證、轉換和分析至關重要。

當您需要以下動作時,請使用字串函式:

字串函式提供完整的文字操控功能,可讓您根據歷程運算式中的文字內容,進行複雜的資料處理和條件式邏輯。

concat concat

串連兩個字串引數或字串清單。

語法
concat(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
清單 listString
字串 字串
簽章與傳回的型別

concat(<string>,<string>)

concat(<listString>)

傳回字串。

範例

concat("Hello","World")

傳回「HelloWorld」。

concat(["Hello"," ","World"])

傳回「Hello World」。

contain contain

檢查第二個引數字串是否包含在第一個引數字串中。

語法
contain(<parameters>)
參數
  • 字串
簽章與傳回的型別

contain(<string>,<string>)

傳回布林值。

範例

contain("rowing is great", "great")

傳回true。

containIgnoreCase containIgnoreCase

檢查第二個引數字串是否包含在第一個引數字串中,而不考慮大小寫。

語法
containIgnoreCase(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
搜尋的字串 字串
簽章與傳回的型別

containIgnoreCase(<string>,<string>)

傳回布林值。

範例

containIgnoreCase("rowing is great", "GREAT")

傳回true。

endWith endWith

如果第二個引數是第一個引數的尾碼,則傳回true。

語法
endWith(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
尾碼 字串
簽章與傳回的型別

endWith(<string>,<string>)

傳回布林值。

範例

endWith("Hello World", "World")

傳回true。

endWith("Hello World", "Hello")

傳回false。

endWithIgnoreCase endWithIgnoreCase

檢查第一個引數字串是否以特定字串(第二個引數字串)結尾,並未考慮大小寫。

語法
endWithIgnoreCase(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
尾碼 字串
簽章與傳回的型別

endWithIgnoreCase(<string>,<string>)

傳回布林值。

範例

endWithIgnoreCase("rowing is great", "AT")

傳回true。

equalIgnoreCase equalIgnoreCase

比較第一個引數字串與第二個引數字串,忽略大小寫考量。

語法
equalIgnoreCase(<parameters>)
參數
  • 字串
簽章與傳回的型別

equalIgnoreCase(<string>,<string>)

傳回布林值。

範例

equalIgnoreCase("rowing is great", "rowing is GREAT")

傳回true。

indexOf indexOf

傳回第二個引數在第一個引數中第一次出現的位置。 如果沒有相符專案,則傳回–1。

語法
indexOf(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
指定值 字串
簽章與傳回的型別

indexOf(<string>,<string>)

傳回整數。

範例

indexOf("Hello", "l")

傳回2。

說明:

在「Hello」中,「l」的第一次出現位置為位置2。

IsEmpty isEmpty

如果引數中的字串沒有字元,則傳回true。

語法
isEmpty(<parameters>)
參數
  • 字串
簽章與傳回的型別

isEmpty(<string>)

傳回布林值。

範例

isEmpty("")

傳回true。

isEmpty("Hello World")

傳回false。

isEmpty(<null>)

傳回false。

isNotEmpty isNotEmpty

如果引數中的字串不是空的,則傳回true。

語法
isNotEmpty(<parameters>)
參數
  • 字串
簽章與傳回的型別

isNotEmpty(<string>)

傳回布林值。

範例

isNotEmpty("")

傳回false。

isNotEmpty("hello")

傳回true。

lastIndexOf lastIndexOf

傳回第二個引數最後一次出現的位置(在第一個引數中)。 如果沒有相符專案,則傳回–1。

語法
lastIndexOf(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
指定值 字串
簽章與傳回的型別

lastIndexOf(<string>,<string>)

傳回整數。

範例

lastIndexOf("Hello", "l")

傳回3。

說明:

在「Hello」中,「l」的最後一次出現位於位置3。

長度 length

傳回引數中字串運算式的字元數。

語法
length(<parameters>)
參數
  • 字串
簽章與傳回的型別

length(<string>)

傳回整數。

範例

length("Hello World")

傳回11。

lower lower

傳回小寫版本的引數。

語法
lower(<parameter>)
參數
  • 字串
簽章與傳回的型別

lower(<string>)

傳回字串。

範例

lower("A")

傳回「a」。

matchRegExp matchRegExp

如果第一個引數中的字串符合第二個引數中的規則運算式,則傳回true。 如需詳細資訊,請參閱此頁面

語法
matchRegExp(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
regexp 字串
簽章與傳回的型別

matchRegExp(<string>,<string>)

傳回布林值。

範例

matchRegExp("12345", "\\d+")

傳回true。

notEqualIgnoreCase notEqualIgnoreCase

檢查第一個引數字串是否與第二個引數字串不同,忽略大小寫考量。

語法
notEqualIgnoreCase(<parameters>)
參數
  • 字串
簽章與傳回的型別

notEqualIgnoreCase(<string>,<string>)

傳回布林值。

範例
notEqualIgnoreCase(@event{iOSPushPermissionAllowed.device.model}, "iPad")

replace replace

以基礎字串中的取代字串取代符合目標字串的第一個專案。

例如,取代會從字串的開頭到結尾進行,將字串「aaa」中的「aa」取代為「b」將會產生「ba」而非「ab」。

語法
replace(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2
參數 類型
基底 字串
Target 字串(RegExp)
取代 字串
簽章與傳回的型別

replace(<base>,<target>,<replacement>)

傳回字串。

範例

replace("Hello World", "l", "x")

傳回「Hexlo World」。

具有RegExp的​範例:

由於目標引數是RegExp,因此根據您要取代的字串,您可能需要將部分字元逸出。 其範例如下:

  • 要評估的字串: |OFFER_A|OFFER_B
  • 由設定檔屬性#{ExperiencePlatform.myFieldGroup.profile.myOffers}提供
  • 要取代的字串: |OFFER_A
  • 字串取代為: ''
  • 您必須在|字元前新增\\

運算式為:

replace(#{ExperiencePlatform.myFieldGroup.profile.myOffers}, '\\|OFFER_A', '')

傳回的字串是: |OFFER_B

您也可以建置要由指定屬性取代的字串:

replace(#{ExperiencePlatform.myFieldGroup.profile.myOffers}, '\\|' + #{ExperiencePlatform.myFieldGroup.profile.myOfferCode}, '')

replaceAll replaceAll

以基礎字串中的取代字串取代所有符合目標字串的相符專案。

例如,取代會從字串的開頭到結尾進行,將字串「aaa」中的「aa」取代為「b」將會產生「ba」而非「ab」。

語法
replaceAll(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2
參數 類型
基底 字串
Target 字串(RegExp)
取代 字串
簽章與傳回的型別

replaceAll(<baseString>,<sourceString>,<replacementString>)

傳回字串。

範例

replaceAll("Hello World", "l", "x")

傳回「Hexxo Worxd」。

由於目標引數是RegExp,因此根據您要取代的字串,您可能需要將部分字元逸出。 請參考取代函式中的範例。

split split

以分隔字串分割第一個引數字串(第二個引數字串,可以是規則運算式),以產生字串(代號)清單。

語法
split(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
輸入字串 字串
分隔符號字串 字串
簽章與傳回型別

split(<input string>, <separator string>)

傳回listString。

範例

split("A_B_C", "_")

傳回["A","B","C"]

具有下列值的事件欄位「event.appVersion」範例:「20.45.2.3434」

split(@event{event.appVersion}, "\\.")

傳回["20", "45", "2", "3434"]

startWith startWith

如果第二個引數是第一個引數的前置詞,則傳回true。

語法
startWith(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
前置詞 字串
簽章與傳回的型別

startWith(<string>,<string>)

傳回布林值。

範例

startWith("Hello World", "Hello")

傳回true。

startWith("Hello World", "World")

傳回false。

startWithIgnoreCase startWithIgnoreCase

如果第二個引數是第一個引數的前置詞,則傳回true,而不考慮大小寫。

語法
startWithIgnoreCase(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
字串 字串
前置詞 字串
簽章與傳回的型別

startWithIgnoreCase(<string>,<string>)

傳回布林值。

範例

startWithIgnoreCase("rowing is great", "RO")

傳回true。

substr substr

傳回開始索引和結束索引之間的字串運算式的子字串。 如果未定義結束索引,則它介於開始索引和結束索引之間。

語法
substr(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2
參數 類型
字串 字串
beginindex 整數
endIndex 整數
簽章與傳回的型別

substr(<string>,<beginIndex>)

substr(<string>,<beginIndex>,<endIndex>)

傳回字串。

範例

substr("Hello World",6)

傳回「World」。

substr("Hello World", 0, 5)

傳回「Hello」。

trim trim

移除開始和結束空格。

語法
trim(<parameters>)
參數
table 0-row-2 1-row-2
參數 類型
字串 字串
簽章與傳回的型別

trim(<string>)

傳回字串。

範例

trim(" Hello ")

傳回「Hello」。

upper upper

傳回引數的大寫版本。

語法
upper(<parameters>)
簽章與傳回的型別

upper(<string>)

傳回字串。

範例

upper("b")

傳回「B」。

UUID uuid

產生隨機UUID (通用唯一識別碼)。

語法
uuid()
參數
此函式不需要引數。
簽章與傳回的型別

uuid()

傳回字串。

範例

uuid()

傳回「79e70b7f-8a85-400b-97a1-9f9826121553」。

AI知識參考

本節包含結構化知識,用於支援與本主題相關的解譯、擷取和問答。

如需完整瞭解,此資訊應結合本頁的檔案。 兩者皆非獨立來源;頁面說明功能,本節提供額外內容,以協助去除術語、意圖、適用性和限制條件的歧義。

  • TL;DR:​此頁面記錄了AJO歷程運算式中可用的所有字串函式,涵蓋文字搜尋、比較、轉換、擷取、驗證、取代、分割和產生唯一識別碼。

意圖:

  • 使用concat串連兩個或多個字串
  • 使用containcontainIgnoreCase搜尋字串中的子字串(區分大小寫或不區分大小寫)
  • 比較兩個字串,但忽略使用equalIgnoreCasenotEqualIgnoreCase的大小寫
  • 使用startWithendWith及其不區分大小寫的變體,檢查字串的開頭或結尾是否為特定首碼或尾碼
  • 使用substr依索引位置擷取子字串
  • 使用replacereplaceAll取代字串中模式的第一次或所有事件
  • 使用split以分隔符號將字串分割為權杖清單
  • 使用uuid針對唯一識別碼需求產生隨機UUID
  • 使用isEmptyisNotEmpty檢查字串是否為空白或非空白

字彙表:

  • RegExp:在replacereplaceAllmatchRegExp中當作目標引數使用的規則運算式模式 — 特殊字元必須以\\逸出
  • UUID:通用唯一識別碼 — uuid()傳回的隨機產生字串識別碼
  • substr:透過指定開始索引和選擇性結束索引(以零為基礎)來擷取字串的一部分

護欄:

  • replacereplaceAll中的target引數視為RegExp;特殊字元(如|.)必須以\\逸出
  • replace僅取代第一個相符專案;使用replaceAll取代每個專案
  • isEmpty傳回False作為Null值(不是True);Null不被視為空字串
  • 找不到相符專案時,indexOflastIndexOf會傳回–1
  • 字串索引位置以零為基礎(第一個字元位於位置0)

術語:

  • 正式名稱:字串函式 — 首字母縮寫:none — 變體:文字函式,字串操作函式
  • 同義字: “contain” = “substring check”;“split” = “tokenize string”;“trim” = “strip whitespace”
  • 請勿混淆:「replace」(僅限第一次發生)≠「replaceAll」(所有發生)
  • 請勿混淆:「indexOf」(第一個發生位置)≠「lastIndexOf」(最後一個發生位置)
  • 請勿混淆:「isEmpty」(僅對零長度字串為true)≠null檢查(對null,isEmpty會傳回false)
  • 請勿混淆:「equalIgnoreCase」(在相等忽略大小寫時傳回true)≠「notEqualIgnoreCase」(在不同的忽略大小寫時傳回true)

常見問題集:

  • 問:如何檢查字串是否包含子字串(不論大小寫為何)? — 使用containIgnoreCase("myString", "searchTerm"),如果在任何情況下都能找到搜尋字詞,則會傳回true。
  • 問:replacereplaceAll之間有何差異?replace僅取代第一個相符專案;replaceAll取代字串中的每個專案。
  • 問:為什麼我需要在replace中逸出|字元? — 目標引數會被視為規則運算式;|是特殊的RegExp字元,必須以\\|逸出,才能被視為常值管道。
  • 問: isEmpty是否為null傳回true? — No,isEmpty傳回False代表Null;它只傳回True代表零長度字串""
  • 問:如何從「20.45.2.3434」之類的版本字串擷取主要版本號碼? — 使用getListItem(split(@event{event.appVersion}, "\\."), 0)以點分割並擷取第一個元素。
  • 問:如何在歷程運算式中產生唯一識別碼? — 使用uuid(),這會傳回隨機產生的UUID字串,而不需要引數。
recommendation-more-help
journey-optimizer-help