99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

VB.Net - 字符串

2022-05-24 15:53 更新

在VB.Net中,可以使用字符串作為字符數(shù)組,但是更常見的做法是使用String關鍵字聲明一個字符串變量。 string關鍵字是System.String類的別名。

創(chuàng)建一個字符串對象

您可以使用以下方法之一創(chuàng)建字符串對象:

  •  通過指定一個字符串給一個字符串變量 

  •  通過使用String類構造函數(shù)

  • 通過使用字符串連接運算符(+) 

  • 通過檢索屬性或調用返回字符串的方法

  • 通過調用格式化方法將值或對象轉換為其字符串表示形式

下面的例子說明了這一點:

Module strings
   Sub Main()
      Dim fname, lname, fullname, greetings As String
      fname = "Rowan"
      lname = "Atkinson"
      fullname = fname + " " + lname
      Console.WriteLine("Full Name: {0}", fullname)

     'by using string constructor
      Dim letters As Char() = {"H", "e", "l", "l", "o"}
      greetings = New String(letters)
      Console.WriteLine("Greetings: {0}", greetings)

      'methods returning String
      Dim sarray() As String = {"Hello", "From", "Tutorials", "Point"}
      Dim message As String = String.Join(" ", sarray)
      Console.WriteLine("Message: {0}", message)

      'formatting method to convert a value 
      Dim waiting As DateTime = New DateTime(2012, 12, 12, 17, 58, 1)
      Dim chat As String = String.Format("Message sent at {0:t} on {0:D}", waiting)
      Console.WriteLine("Message: {0}", chat)
      Console.ReadLine()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生了以下結果:

Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, December 12, 2012

String類的屬性

String類有以下兩個屬性:

SN屬性名稱和說明
1

Chars

獲取當前String對象中指定位置的Char對象。

2

Length

獲取當前String對象中的字符數(shù)。

String類的方法

String類有許多方法可以幫助你處理字符串對象。 下表提供了一些最常用的方法:

SN方法名稱和說明
1

Public Shared Function Compare ( strA As String, strB As String ) As Integer
公共共享函數(shù)比較(strA As String,strB As String)As Integer

比較兩個指定的字符串對象,并返回一個整數(shù),指示它們在排序順序中的相對位置。

2

Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer
公共共享函數(shù)比較(strA As String,strB As String,ignoreCase As Boolean)As Integer

比較兩個指定的字符串對象,并返回一個整數(shù),指示它們在排序順序中的相對位置。 但是,如果布爾參數(shù)為true,它將忽略大小寫。

3

Public Shared Function Concat ( str0 As String, str1 As String ) As String
公共共享函數(shù)Concat(str0 As String,str1 As String)As String

連接兩個字符串對象。

4

Public Shared Function Concat ( str0 As String, str1 As String, str2 As String ) As String
公共共享函數(shù)Concat(str0 As String,str1 As String,str2 As String)As String

連接三個字符串對象。

5

Public Shared Function Concat ( str0 As String, str1 As String, str2 As String, str3 As String ) As String公共共享函數(shù)Concat(str0 As String,str1 As String,str2 As String,str3 As String)As String

連接四個字符串對象。

6

Public Function Contains ( value As String ) As Boolean
公共函數(shù)包含(值作為字符串)As Boolean

返回一個值,指示指定的字符串對象是否出現(xiàn)在此字符串中。

7

Public Shared Function Copy ( str As String ) As String
公共共享函數(shù)復制(str作為字符串)As String

創(chuàng)建與指定字符串具有相同值的新String對象。

8

pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer )

將指定數(shù)量的字符從字符串對象的指定位置復制到Unicode字符數(shù)組中的指定位置。

9

Public Function EndsWith ( value As String ) As Boolean
公共函數(shù)endsWith(值作為字符串)As Boolean

確定字符串對象的結尾是否與指定的字符串匹配。

10

Public Function Equals ( value As String ) As Boolean
公共函數(shù)等于(值作為字符串)As Boolean

確定當前字符串對象,并指定字符串對象是否具有相同的值。

11

Public Shared Function Equals ( a As String, b As String ) As Boolean
公共共享函數(shù)等于(A作為字符串,B作為字符串)As Boolean

確定兩個指定字符串對象是否具有相同的值。

12

Public Shared Function Format ( format As String, arg0 As Object ) As String
公共共享函數(shù)格式(格式作為字符串,arg0作為對象)As String

將指定字符串中的一個或多個格式項替換為指定對象的字符串表示形式。

13

Public Function IndexOf ( value As Char ) As Integer
公共函數(shù)IndexOf(價值為CHAR)As Integer

返回當前字符串中指定Unicode字符第一次出現(xiàn)的從零開始的索引。

14

Public Function IndexOf ( value As String ) As Integer
公共函數(shù)IndexOf(值作為字符串)As Integer

返回此實例中指定字符串第一次出現(xiàn)的從零開始的索引。

15

Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer
公共函數(shù)IndexOf(值為char,則startIndex作為整數(shù)) As Integer

返回此字符串中指定Unicode字符第一次出現(xiàn)的從零開始的索引,從指定的字符位置開始搜索。

16

Public Function IndexOf ( value As String, startIndex As Integer ) As Integer
公共函數(shù)IndexOf(值作為字符串,則startIndex作為整數(shù))As Integer

返回此實例中指定字符串第一次出現(xiàn)的從零開始的索引,開始在指定的字符位置搜索。

17

Public Function IndexOfAny ( anyOf As Char() ) As Integer
公共函數(shù)IndexOfAny(anyOf為CHAR())As Integer

返回在指定的Unicode字符數(shù)組中的任何字符的此實例中第一次出現(xiàn)的從零開始的索引。

18

Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer
公共函數(shù)IndexOfAny(anyOf為char(),則startIndex作為整數(shù))As Integer

返回在指定的Unicode字符數(shù)組中的任意字符的此實例中第一次出現(xiàn)的從零開始的索引,開始在指定的字符位置搜索。

19

Public Function Insert ( startIndex As Integer, value As String ) As String
公共函數(shù)Insert(的startIndex為整數(shù),值作為字符串)As String

返回一個新字符串,其中指定的字符串插入到當前字符串對象中指定的索引位置。

20

Public Shared Function IsNullOrEmpty ( value As String ) As Boolean
公共共享函數(shù)IsNullOrEmpty(值作為字符串) As Boolean

指示指定的字符串是空還是空字符串。

21

Public Shared Function Join ( separator As String, ParamArray value As String() ) As String
公共共享函數(shù)加入(分隔符作為字符串,值的ParamArray作為字符串())As String

連接字符串數(shù)組的所有元素,使用每個元素之間指定的分隔符。

22

Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String
公共共享函數(shù)加入(分隔符作為字符串,值作為字符串(),則startIndex整數(shù),算作整數(shù))As String

使用每個元素之間指定的分隔符連接字符串數(shù)組的指定元素。

23

Public Function LastIndexOf ( value As Char ) As Integer
公共函數(shù)LastIndexOf(價值為CHAR) As Integer

返回當前字符串對象中指定Unicode字符的最后一次出現(xiàn)的從零開始的索引位置。

24

Public Function LastIndexOf ( value As String ) As Integer
公共函數(shù)LastIndexOf(值作為字符串) As Integer

返回當前字符串對象中指定字符串的最后一次出現(xiàn)的從零開始的索引位置。

25

Public Function Remove ( startIndex As Integer )As String
字符串作為公共函數(shù)刪除 (startIndex 作為整數(shù))As String

刪除當前實例中的所有字符,從指定位置開始,并繼續(xù)到最后一個位置,并返回字符串。

26

Public Function Remove ( startIndex As Integer, count As Integer ) As String
字符串作為公共函數(shù)刪除 (startIndex 作為整數(shù)作為整數(shù)計數(shù))As String

從指定位置開始刪除當前字符串中指定數(shù)量的字符,并返回字符串。

27

Public Function Replace ( oldChar As Char, newChar As Char ) As String
公共函數(shù)替換(oldChar為char,newChar為CHAR)As String

用指定的Unicode字符替換當前字符串對象中指定Unicode字符的所有出現(xiàn),并返回新字符串。

28

Public Function Replace ( oldValue As String, newValue As String ) As String
公共功能替換(屬性oldValue作為字符串,newValue作為字符串)As String

將當前字符串對象中指定字符串的所有出現(xiàn)替換為指定的字符串,并返回新字符串。

29

Public Function Split ( ParamArray separator As Char() ) As String()
公共函數(shù)拆分(ParamArray分隔符作為Char())As String()

返回一個字符串數(shù)組,其中包含當前字符串對象中的子字符串,由指定的Unicode字符數(shù)組的元素分隔。

30

Public Function Split ( separator As Char(), count As Integer ) As String()
公共函數(shù)分割(分隔符為char(),計數(shù)為整數(shù))As String()

返回一個字符串數(shù)組,其中包含當前字符串對象中的子字符串,由指定的Unicode字符數(shù)組的元素分隔。 int參數(shù)指定要返回的子字符串的最大數(shù)目。

31

Public Function StartsWith ( value As String ) As Boolean
公共函數(shù)StartsWith(值作為字符串)As Boolean

確定此字符串實例的開頭是否與指定的字符串匹配。

32

Public Function ToCharArray As Char()
公共函數(shù)ToCharArray As CHAR()

返回包含當前字符串對象中所有字符的Unicode字符數(shù)組。

33

Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char()
公共函數(shù)ToCharArray(startIndex為整數(shù),length為整數(shù)) As Char()

返回一個Unicode字符數(shù)組,其中包含當前字符串對象中的所有字符,從指定的索引開始,直到指定的長度。

34

Public Function ToLower As String
公共函數(shù)ToLower As String

返回此字符串的轉換為小寫的副本。

35

Public Function ToUpper As String
公共函數(shù)ToUpper As String

返回此字符串的轉換為大寫的副本。

36

Public Function Trim As String
公共函數(shù)Trim As String

從當前String對象中刪除所有前導和尾部空格字符。

上面列出的方法并不詳盡,請訪問MSDN庫獲取完整的方法列表和String類構造函數(shù)。

例子:

下面的例子說明了上述一些方法:

比較字符串:

Module strings
   Sub Main()
      Dim str1, str2 As String
      str1 = "This is test"
      str2 = "This is text"
      If (String.Compare(str1, str2) = 0) Then
          Console.WriteLine(str1 + " and " + str2 +
                            " are equal.")
      Else
          Console.WriteLine(str1 + " and " + str2 +
                            " are not equal.")
      End If
      Console.ReadLine()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生了以下結果:

This is test and This is text are not equal.

字符串包含字符串:

Module strings
   Sub Main()
      Dim str1 As String
      str1 = "This is test"
      If (str1.Contains("test")) Then
          Console.WriteLine("The sequence 'test' was found.")
      End If
      Console.ReadLine()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生了以下結果:

The sequence 'test' was found.

獲取的子字符串:

Module strings
   Sub Main()
      Dim str As String
      str = "Last night I dreamt of San Pedro"
      Console.WriteLine(str)
      Dim substr As String = str.Substring(23)
      Console.WriteLine(substr)
      Console.ReadLine()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生了以下結果:

Last night I dreamt of San Pedro
San Pedro.

加入字符串:

Module strings
   Sub Main()
      Dim strarray As String() = {"Down the way where the nights are gay",
                          "And the sun shines daily on the mountain top",
                           "I took a trip on a sailing ship",
                          "And when I reached Jamaica",
                          "I made a stop"}
      Dim str As String = String.Join(vbCrLf, strarray)
      Console.WriteLine(str)
      Console.ReadLine()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生了以下結果:

Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號