PowerApps 中的 EndsWith 和 StartsWith 函式EndsWith and StartsWith functions in PowerApps

測試某個文字字串的開頭或結尾是否為另一個文字字串。Tests whether a text string begins or ends another text string.

說明Description

EndsWith 函式會測試某個文字字串的結尾是否為另一個文字字串。The EndsWith function tests whether one text string ends with another.

StartsWith 函式會測試某個文字字串的開頭是否為另一個文字字串。The StartsWith function tests whether one text string begins with another.

對於這兩個函式,測試不區分大小寫。For both functions, the tests are case insensitive. 兩者的傳回值都是布林值 truefalseThe return value of both is a Boolean true or false.

使用 EndsWithStartsWith 搭配 Filter 函式可搜尋應用程式內的資料。Use EndsWith and StartsWith with the Filter function to search the data within your app. 您也可以使用 in 運算子或 Search 函式來查看文字字串內的任何位置,而不只是查看開頭或結尾。You can also use the in operator or the Search function to look anywhere within text strings, not just at the beginning or end. 您所選擇的函式取決於您應用程式的需求,以及您的特定資料來源可以委派哪一個函式。Your choice of functions will depend on the needs of your app and which function can be delegated for your particular data source. 如果這些函式之中有一個無法委派,在您編寫時會出現藍點來警告您有這項限制。If one of these functions can't be delegated, a blue dot will appear at authoring time to warn you of this limitation.

語法Syntax

EndsWith( Text, EndText )EndsWith( Text, EndText )

  • Text – 必要項目。Text – Required. 要測試的文字。The text to test.
  • EndText – 必要項目。EndText – Required. 要在 Text 結尾搜尋的文字。The text to search for at the end of Text. 如果 EndText 為空白字串,EndsWith 會傳回 true。If EndText is an empty string, EndsWith returns true.

StartsWith( Text, StartText )StartsWith( Text, StartText )

  • Text – 必要項目。Text – Required. 要測試的文字。The text to test.
  • StartText – 必要項目。StartText – Required. 要在 Text 開頭搜尋的文字。The text to search for at the beginning of Text. 如果 StartText 為空白字串,StartsWith 會傳回 trueIf StartText is an empty string, StartsWith returns true.

範例Examples

公式Formula 說明Description 結果Result
EndsWith( "Hello World", "world" )EndsWith( "Hello World", "world" ) 測試 "Hello World" 是否以 "world" 為結尾。Tests whether "Hello World" ends with "world". 此測試不區分大小寫。The test is case insensitive. truetrue
EndsWith( "Good bye", "good" )EndsWith( "Good bye", "good" ) 測試 "Good bye" 是否以 "good" 為結尾。Tests whether "Good bye" ends with "good". EndText 引數 ("good") 出現在文字中,但不是在結尾。The EndText argument ("good") appears in the text but not at the end. falsefalse
EndsWith( "Always say hello", "hello" )EndsWith( "Always say hello", "hello" ) 測試 "Always say hello" 是否以 "hello" 為結尾。Tests whether "Always say hello" ends with "hello". truetrue
Endswith( "Bye bye", "" )Endswith( "Bye bye", "" ) 測試 "Bye bye" 是否以空白文字字串為結尾 (Len 會傳回 0)。Tests whether "Bye bye" ends with an empty text string (Len returns 0). Filter 運算式中更好使用,此情況下 EndsWith 定義會傳回 trueEasing its use in Filter expressions, EndsWith is defined to return true in this case. truetrue
公式Formula 說明Description 結果Result
StartsWith( "Hello World", "hello" )StartsWith( "Hello World", "hello" ) 測試 "Hello World" 的開頭是否為 "hello"Tests whether "Hello World" begins with "hello". 此測試不區分大小寫。The test is case insensitive. truetrue
StartsWith( "Good bye", "hello" )StartsWith( "Good bye", "hello" ) 測試 "Good bye" 的開頭是否為 "hello"Tests whether "Good bye" begins with "hello". falsefalse
StartsWith( "Always say hello", "hello" )StartsWith( "Always say hello", "hello" ) 測試 "Always say hello" 的開頭是否為 "hello"Tests whether "Always say hello" begins with "hello". 雖然 "hello" 出現在文字中,但不是在開頭出現。Although "hello" appears in the text, it doesn't appear at the beginning. falsefalse
StartsWith( "Bye bye", "" )StartsWith( "Bye bye", "" ) 測試 "Bye bye" 是否以空白文字字串為開頭 (Len 會傳回 0)。Tests whether "Bye bye" starts with an empty text string (Len returns 0). Filter 運算式中更好使用,此情況下 StartsWith 定義會傳回 trueEasing its use in Filter expressions, StartsWith is defined to return true in this case. truetrue

搜尋使用者體驗Search user experience

在許多應用程式中,您可以在搜尋方塊中輸入一或多個字元,以篩選大型資料集內的記錄清單。In many apps, you can type one or more characters into a search box to filter a list of records in a large data set. 當您輸入時,此清單只會顯示符合搜尋條件的記錄。As you type, the list shows only those records that match the search criteria.

本主題其餘部分的範例會顯示搜尋客戶清單的結果,該清單中包含此資料︰The examples in the rest of this topic show the results of searching a Customers list that contains this data:

若要將此資料來源建立為集合,請建立按鈕控制項,並將其 OnSelect 屬性設定為下列公式︰To create this data source as a collection, create a Button control and set its OnSelect property to this formula:

ClearCollect( Customers, Table( { Name: "Fred Garcia", Company: "Northwind Traders" }, { Name: "Cole Miller", Company: "Contoso" }, { Name: "Glenda Johnson", Company: "Contoso" }, { Name: "Mike Collins", Company: "Adventure Works" }, { Name: "Colleen Jones", Company: "Adventure Works" } ) )ClearCollect( Customers, Table( { Name: "Fred Garcia", Company: "Northwind Traders" }, { Name: "Cole Miller", Company: "Contoso" }, { Name: "Glenda Johnson", Company: "Contoso" }, { Name: "Mike Collins", Company: "Adventure Works" }, { Name: "Colleen Jones", Company: "Adventure Works" } ) )

和此範例一樣,您可以在畫面底部的資源庫控制項中顯示記錄清單。As in this example, you can show a list of records in a Gallery control at the bottom of a screen. 在靠近畫面頂端的地方,您可以新增名為 SearchInput文字輸入控制項,讓使用者可以指定其感興趣的記錄。Near the top of the screen, you can add a Text input control, named SearchInput, so that users can specify which records interest them.

當使用者在 SearchInput 中輸入字元時,資源庫中的結果會自動經過篩選。As the user types characters in SearchInput, the results in the gallery are automatically filtered. 在此案例中,資源庫會設定為顯示客戶名稱 (而非公司名稱) 開頭為 SearchInput 中字元序列的記錄。如果使用者在搜尋方塊中輸入 co,資源庫會顯示下列結果︰In this case, the gallery is configured to show records for which the name of the customer (not the name of the company) starts with the sequence of characters in SearchInput.If the user types co in the search box, the gallery shows these results:

若要根據名稱資料行進行篩選,請將資源庫控制項的 Items 屬性設定為下列其中一個公式︰To filter based on the Name column, set the Items property of the gallery control to one of these formulas:

公式Formula 說明Description 結果Result
Filter( Customers, StartsWith( Name, SearchInput.Text ) )Filter( Customers, StartsWith( Name, SearchInput.Text ) ) 篩選客戶資料來源中,名稱資料行開頭出現此搜尋字串的記錄。Filters the Customers data source for records in which the search string appears at the start of the Name column. 此測試不區分大小寫。The test is case insensitive. 如果使用者在搜尋方塊中輸入 co,資源庫會顯示 Colleen JonesCole MillerIf the user types co in the search box, the gallery shows Colleen Jones and Cole Miller. 資源庫不會顯示 Mike Collins,因為該筆記錄的名稱資料行開頭不是此搜尋字串。The gallery doesn't show Mike Collins because the Name column for that record doesn't start with the search string.
Filter( Customers, SearchInput.Text in Name )Filter( Customers, SearchInput.Text in Name ) 篩選客戶資料來源中,名稱資料行任意處出現此搜尋字串的記錄。Filters the Customers data source for records in which the search string appears anywhere in the Name column. 此測試不區分大小寫。The test is case insensitive. 如果使用者在搜尋方塊中輸入 co,資源庫會顯示 Colleen JonesCole MillerMike Collins,因為這些記錄的名稱資料行某處皆出現此搜尋字串。If the user types co in the search box, the gallery shows Colleen Jones, Cole Miller, and Mike Collins because the search string appears somewhere in the Name column of all of those records.
Search( Customers, SearchInput.Text, "Name" )Search( Customers, SearchInput.Text, "Name" ) 類似於使用 in 運算子,Search 函式會搜尋每一筆記錄的名稱資料行內任意處是否有相符項目。Similar to using the in operator, the Search function searches for a match anywhere within the Name column of each record. 請注意,您必須以雙引號括住資料行名稱。Note that you must enclose the column name in double quotation marks.

您可以擴大搜尋,將公司資料行以及名稱資料行納入其中︰You can expand your search to include the Company column as well as the Name column:

公式Formula 說明Description 結果Result
Filter( Customers, StartsWith( Name, SearchInput.Text ) || StartsWith( Company, SearchInput.Text ) )Filter( Customers, StartsWith( Name, SearchInput.Text ) || StartsWith( Company, SearchInput.Text ) ) 篩選客戶資料來源中,名稱資料行或公司資料行開頭為此搜尋字串 (例如,co) 的記錄。Filters the Customers data source for records in which either the Name column or the Company column starts with the search string (for example, co). 若任一 StartsWith 函式為 true,則 || 運算子trueThe || operator is true if either StartsWith function is true.
Filter( Customers, SearchInput.Text in Name || SearchInput.Text in Company )Filter( Customers, SearchInput.Text in Name || SearchInput.Text in Company ) 篩選客戶資料來源中,名稱資料行或公司資料行內的任意處包含此搜尋字串 (例如,co) 的記錄。Filters the Customers data source for records in which either the Name column or the Company column contains the search string (for example, co) anywhere within it.
Search( Customers, SearchInput.Text, "Name", "Company" )Search( Customers, SearchInput.Text, "Name", "Company" ) 類似於使用 in 運算子,Search 函式會搜尋客戶資料來源中,名稱資料行或公司資料行內的任意處包含此搜尋字串 (例如,co) 的記錄。Similar to using the in operator, the Search function searches the Customers data source for records in which either the Name column or the Company column contains the search string (for example, co) anywhere within it. 如果您想要指定多個資料行和多個 in 運算子,Search 函式會比 Filter 更容易理解和撰寫。The Search function is easier to read and write than Filter if you want to specify multiple columns and multiple in operators. 請注意,您必須以雙引號括住資料行名稱。Note that you must enclose the names of the columns in double quotation marks.