Skip to content

字串 String

YenLin Wu edited this page Oct 22, 2020 · 4 revisions
  • 字串長度 Length of the String

int_Length = str_Variable.Length
e.g. "ABCDE".Length -> 5
e.g. "ABCD E".Length -> 6

  • 擷取部份字串 Get substring from a String

(1) 透過 index 取得單一字串

str_SubString = str_Variable.Substring( start index, # letters )
e.g. "ABCDE"( 1 ) -> "B"

(2) Substring( ) 函數

str_SubString = str_Variable.Substring( start index, # letters )
e.g. "ABCDE".Substring( 1, 2 ) -> "BC"
e.g. "ABCDE".Substring( 1 ) -> "BCDE"

(3) Right( )、Left( ) 函數

str_Substring = Right( str_Variable, # letters )
str_Substring = Left( str_Variable, # letters )
e.g. Right( "ABCDE", 2 ) -> "DE"
e.g. Left( "ABCDE", 2 ) -> "AB"

  • 取代字串中的部份內容 Replace in String

str_NewContent = str_Variable.Replace( "被取代的字串", "取代後的字串" )
e.g. "ABCDE".Replace( "BCD", "XYZ" ) -> "AXYZE"

  • 刪除字串中的部份內容 Remove characters from a String

str_NewContent = str_Variable.Remove( start index, # letters )
e.g. "ABCDE".Remove( 1, 2 ) -> "ADE"
e.g. "ABCDE".Remove( 1 ) -> "A"

  • 刪除字串的前後空格 Remove all leading and trailing white-space character

str_Variable = Strings.Trim( str_Variable )
e.g. Strings.Trim( "A B C D" ) -> "A B C D"
e.g. Strings.Trim( "A BCD " ) -> "A BCD"
e.g. Strings.Trim( " ABC D" ) -> "ABC D"

  • 判斷字串是否為空值或空白 Check whether the specified string is null, white-space, or an Empty string

(1)

String.IsNullOrEmpty( str_Variable )
e.g. String.IsNullOrEmpty( "" ) -> True (布林值)
e.g. String.IsNullOrEmpty( " " ) -> False (布林值)

(2)

String.IsNullOrWhiteSpace( str_Variable )
e.g. String.IsNullOrWhiteSpace( "" ) -> True (布林值)
e.g. String.IsNullOrWhiteSpace( " " ) -> True (布林值)

  • 判斷字串是否包含特定內容 Check whether the specified substring occurs within a given string or not

str_Variable.Contains( "特定的字串內容" )
e.g. "ABCDE".Contains( "BCD" ) -> True (布林值)
e.g. "ABCDE".Contains( "xyz" ) -> False (布林值)

  • 判斷兩字串是否相同 Determine whether two String objects have the same value or not

str_Variable1.Equals( str_Variable2 )
e.g. "ABC".Equals( "ABCDE" ) -> False (布林值)
e.g. "ABC".Equals( "A B C" ) -> False (布林值)
e.g. "ABC".Equals( "abc" ) -> False (布林值)