当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

VB.Net - 字符串

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:58:58

由 yiyohunter 创建,youj 最后一次修改 2016-12-12 在VB.Net中,可以使用字符串作为字符数组,但是更常见的做法是使用String关键字声明一个字符串变量。 string关键字是System.String类的别名。 创建一个字符串对象您可以使用以下方法之一创建字符串对象: By assigning a string literal to a String variable 通过指定一个字符串给一个字符串变量 By using a String class constructor 通过使用String类构造函数By using the string concatenation operator (+) 通过使用字符串连接运算符(+) By retrieving a property or calling a method that returns a string 通过检索属性或调用返回字符串的方法By calling a formatting method to convert a value or object to its string representation通过调用格式化方法将值或对象转换为其字符串表示形式下面的例子说明了这一点: 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 SubEnd Module当上述代码被编译和执行时,它产生了以下结果: Full Name: Rowan AtkinsonGreetings: HelloMessage: Hello From Tutorials PointMessage: Message sent at 5:58 PM on Wednesday, December 12, 2012 String类的属性 String类有以下两个属性: SN属性名称和说明1 Chars 获取当前String对象中指定位置的Char对象。 2Length获取当前String对象中的字符数。 String类的方法 String类有许多方法可以帮助你处理字符串对象。 下表提供了一些最常用的方法: SN方法名称和说明1 Public Shared Function Compare ( strA As String, strB As String ) As Integer公共共享函数比较(strA As String,strB As String)As Integer 比较两个指定的字符串对象,并返回一个整数,指示它们在排序顺序中的相对位置。 2 Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer公共共享函数比较(strA As String,strB As String,ignoreCase As Boolean)As Integer 比较两个指定的字符串对象,并返回一个整数,指示它们在排序顺序中的相对位置。 但是,如果布尔参数为true,它将忽略大小写。 3 Public Shared Function Concat ( str0 As String, str1 As String ) As String公共共享函数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公共共享函数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公共共享函数Concat(str0 As String,str1 As String,str2 As String,str3 As String)As String 连接四个字符串对象。 6 Public Function Contains ( value As String ) As Boolean公共函数包含(值作为字符串)As Boolean 返回一个值,指示指定的字符串对象是否出现在此字符串中。 7 Public Shared Function Copy ( str As String ) As String公共共享函数复制(str作为字符串)As String 创建与指定字符串具有相同值的新String对象。 8 pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer )将指定数量的字符从字符串对象的指定位置复制到Unicode字符数组中的指定位置。 9 Public Function EndsWith ( value As String ) As Boolean公共函数endsWith(值作为字符串)As Boolean 确定字符串对象的结尾是否与指定的字符串匹配。 10 Public Function Equals ( value As String ) As Boolean公共函数等于(值作为字符串)As Boolean 确定当前字符串对象,并指定字符串对象是否具有相同的值。 11 Public Shared Function Equals ( a As String, b As String ) As Boolean公共共享函数等于(A作为字符串,B作为字符串)As Boolean 确定两个指定字符串对象是否具有相同的值。 12 Public Shared Function Format ( format As String, arg0 As Object ) As String公共共享函数格式(格式作为字符串,arg0作为对象)As String 将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式。 13 Public Function IndexOf ( value As Char ) As Integer公共函数IndexOf(价值为CHAR)As Integer 返回当前字符串中指定Unicode字符第一次出现的从零开始的索引。 14 Public Function IndexOf ( value As String ) As Integer公共函数IndexOf(值作为字符串)As Integer 返回此实例中指定字符串第一次出现的从零开始的索引。 15 Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer公共函数IndexOf(值为char,则startIndex作为整数) As Integer 返回此字符串中指定Unicode字符第一次出现的从零开始的索引,从指定的字符位置开始搜索。 16 Public Function IndexOf ( value As String, startIndex As Integer ) As Integer公共函数IndexOf(值作为字符串,则startIndex作为整数)As Integer 返回此实例中指定字符串第一次出现的从零开始的索引,开始在指定的字符位置搜索。

[1] [2] [3]  下一页


VB.Net - 字符串