VB.NET 教程_02_常见对象

VB.Net - 字符串

 

在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 Sub
End Module

 

当上述代码被编译和执行时,它产生了以下结果:

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对象中的字符数。

 

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

返回此实例中指定字符串第一次出现的从零开始的索引,开始在指定的字符位置搜索。

17

Public Function IndexOfAny ( anyOf As Char() ) As Integer
公共函数IndexOfAny(anyOf为CHAR())As Integer

返回在指定的Unicode字符数组中的任何字符的此实例中第一次出现的从零开始的索引。

18

Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer
公共函数IndexOfAny(anyOf为char(),则startIndex作为整数)As Integer

返回在指定的Unicode字符数组中的任意字符的此实例中第一次出现的从零开始的索引,开始在指定的字符位置搜索。

19

Public Function Insert ( startIndex As Integer, value As String ) As String
公共函数Insert(的startIndex为整数,值作为字符串)As String

返回一个新字符串,其中指定的字符串插入到当前字符串对象中指定的索引位置。

20

Public Shared Function IsNullOrEmpty ( value As String ) As Boolean
公共共享函数IsNullOrEmpty(值作为字符串) As Boolean

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

21

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

连接字符串数组的所有元素,使用每个元素之间指定的分隔符。

22

Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String
公共共享函数加入(分隔符作为字符串,值作为字符串(),则startIndex整数,算作整数)As String

使用每个元素之间指定的分隔符连接字符串数组的指定元素。

23

Public Function LastIndexOf ( value As Char ) As Integer
公共函数LastIndexOf(价值为CHAR) As Integer

返回当前字符串对象中指定Unicode字符的最后一次出现的从零开始的索引位置。

24

Public Function LastIndexOf ( value As String ) As Integer
公共函数LastIndexOf(值作为字符串) As Integer

返回当前字符串对象中指定字符串的最后一次出现的从零开始的索引位置。

25

Public Function Remove ( startIndex As Integer )As String
字符串作为公共函数删除 (startIndex 作为整数)As String

删除当前实例中的所有字符,从指定位置开始,并继续到最后一个位置,并返回字符串。

26

Public Function Remove ( startIndex As Integer, count As Integer ) As String
字符串作为公共函数删除 (startIndex 作为整数作为整数计数)As String

从指定位置开始删除当前字符串中指定数量的字符,并返回字符串。

27

Public Function Replace ( oldChar As Char, newChar As Char ) As String
公共函数替换(oldChar为char,newChar为CHAR)As String

用指定的Unicode字符替换当前字符串对象中指定Unicode字符的所有出现,并返回新字符串。

28

Public Function Replace ( oldValue As String, newValue As String ) As String
公共功能替换(属性oldValue作为字符串,newValue作为字符串)As String

将当前字符串对象中指定字符串的所有出现替换为指定的字符串,并返回新字符串。

29

Public Function Split ( ParamArray separator As Char() ) As String()
公共函数拆分(ParamArray分隔符作为Char())As String()

返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的Unicode字符数组的元素分隔。

30

Public Function Split ( separator As Char(), count As Integer ) As String()
公共函数分割(分隔符为char(),计数为整数)As String()

返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的Unicode字符数组的元素分隔。 int参数指定要返回的子字符串的最大数目。

31

Public Function StartsWith ( value As String ) As Boolean
公共函数StartsWith(值作为字符串)As Boolean

确定此字符串实例的开头是否与指定的字符串匹配。

32

Public Function ToCharArray As Char()
公共函数ToCharArray As CHAR()

返回包含当前字符串对象中所有字符的Unicode字符数组。

33

Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char()
公共函数ToCharArray(startIndex为整数,length为整数) As Char()

返回一个Unicode字符数组,其中包含当前字符串对象中的所有字符,从指定的索引开始,直到指定的长度。

34

Public Function T

<think> 首先,用户的问题是关于VB.NET中语句`CheckBox_SCloss.CheckState = True`报错的原因及解决方案。用户引用了几个站内参考,我需要参考这些引用来分析问题。 用户的查询中提到: - 引用[1]: 讨论了在VB.NET下设置checkbox选中状态的问题,涉及到使用`SetAttribute("Checked", "true/false")`。 - 引用[2]: 关于VB6升级到VB.NET时CheckBox事件差异。 - 引用[3]: 涉及DataGridViewCheckBoxColumn中checkbox状态改变的处理。 - 引用[4]: 在窗体加载时设置所有CheckBox为选中状态的代码。 用户的核心问题是:语句`CheckBox_SCloss.CheckState = True`报错的原因及解决办法。 我需要回想VB.NET中CheckBox控件的属性。在VB.NET中,CheckBox控件的状态通常使用`Checked`属性来设置,而不是`CheckState`。`CheckState`属性用于表示三态复选框的状态(Checked、Unchecked、Indeterminate)。 - `Checked`属性是一个布尔值(True/False)。 - `CheckState`属性是一个枚举值:`CheckState.Checked`, `CheckState.Unchecked`, `CheckState.Indeterminate`. 用户尝试设置`CheckBox_SCloss.CheckState = True`。但`True`是一个布尔值,而`CheckState`期望的是一个`CheckState`枚举值。这就可能导致类型不匹配错误。 可能的错误原因: 1. **类型不匹配错误**:因为`CheckState`属性不能直接设置为布尔值`True`。它需要设置为`CheckState.Checked`。 2. **对象引用问题**:用户可能忘记初始化`CheckBox_SCloss`控件,或者控件名称错误,导致空引用异常。 3. **上下文问题**:错误可能发生在特定的上下文中,比如在事件处理程序中。 参考提供的引用: - 引用[4]展示了在窗体加载时设置CheckBox的`Checked`属性:`Ck.Checked = True`。这使用了`Checked`属性,而不是`CheckState`。 - 引用[3]处理DataGridView中的CheckBox,使用了`Value`属性来设置状态。 - 引用[1]谈论HTML中的checkbox,但用户的问题是Windows Forms的VB.NET。 用户的问题是关于`CheckBox_SCloss`,这看起来像是一个Windows Forms控件名,不是ASP.NET或HTML。所以,我应该聚焦在Windows Forms上。 解决方案: - 使用`Checked`属性:`CheckBox_SCloss.Checked = True` 来设置选中状态。 - 或者,如果需要使用`CheckState`,设置为枚举值:`CheckBox_SCloss.CheckState = CheckState.Checked`。 常见错误: - 如果将布尔值赋给`CheckState`属性,编译时或运行时会出现类型转换错误,因为VB.NET不支持隐式转换。 用户上一次的问题(在系统指令中)是编程相关,但现在具体到VB.NET的错误分析。 回答结构: - 解释错误原因。 - 提供正确代码。 - 参考用户引用(如果相关)。 - 添加相关问题。 生成相关问题: - 基于用户问题,生成相关VB.NET问题。 确保回答清晰、可靠: - 使用中文。 - 结构:先说明原因,然后解决方案。 - 添加相关问题结尾。 最终回答大纲: 1. **错误原因分析**:解释`CheckState`属性期望枚举值,而不是布尔值。 2. **正确用法**:展示使用`Checked`属性或正确设置`CheckState`。 3. **示例代码**:基于引用[4],提供类似示例。 4. **相关问题**:生成3个相关问题。 </think> ### VB.NET中`CheckBox_SCloss.CheckState = True`报错的原因及解决方案 在VB.NET中,当您尝试执行语句`CheckBox_SCloss.CheckState = True`时,常见的错误包括**编译时错误(如类型不匹配)**或**运行时异常(如InvalidCastException)**。以下是详细分析和解决方案,基于VB.NET的Windows Forms编程规范。 #### 错误原因分析 1. **类型不匹配错误**: - `CheckState`属性是`System.Windows.Forms.CheckState`枚举类型,它有三个值:`Checked`、`Unchecked`和`Indeterminate`(用于三态复选框)。 - `True`是一个布尔值(`Boolean`类型),不能直接赋值给枚举属性。VB.NET不会自动将`Boolean`转换为`CheckState`枚举。 - 错误示例:如果编译器检测到类型不兼容,会报错“Option Strict On disallows implicit conversions from 'Boolean' to 'System.Windows.Forms.CheckState'”。 2. **对象引用问题**: - 如果`CheckBox_SCloss`控件未正确初始化、名称拼写错误或不在当前窗体中,会导致`NullReferenceException`(对象引用未设置为对象的实例)。 - 常见于动态创建控件或事件处理逻辑中。 3. **上下文依赖性问题**: - 在事件处理程序(如`CheckedChanged`)中错误设置状态,可能触发递归事件循环或冲突(参考引用[2]的事件差异说明)[^2]。 #### 解决方案 根据VB.NET最佳实践,修复步骤如下: 1. **使用`Checked`属性代替`CheckState`(推荐)**: - `CheckBox.Checked`属性是布尔类型(`True`/`False`),直接对应选中状态。 - 正确代码: ```vb CheckBox_SCloss.Checked = True ' 选中复选框 CheckBox_SCloss.Checked = False ' 取消选中 ``` - 这种方法简单高效,适用于大多数场景(如引用[4]中窗体加载时批量设置CheckBox)[^4]。 2. **如果必须使用`CheckState`,需指定枚举值**: - 显式使用`CheckState`枚举成员。 - 正确代码: ```vb CheckBox_SCloss.CheckState = CheckState.Checked ' 选中 CheckBox_SCloss.CheckState = CheckState.Unchecked ' 取消选中 CheckBox_SCloss.CheckState = CheckState.Indeterminate ' 不定状态(三态复选框) ``` - **注意**:仅当复选框支持三态时(`ThreeState = True`),才需要`CheckState`。 3. **检查控件初始化和名称**: - 确保`CheckBox_SCloss`在窗体设计器中正确声明,且名称一致。 - 在窗体加载事件中初始化控件(模仿引用[4]): ```vb Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load If CheckBox_SCloss IsNot Nothing Then CheckBox_SCloss.Checked = True ' 使用Checked避免错误 Else MessageBox.Show("CheckBox_SCloss未找到!") End If End Sub ``` 4. **处理事件冲突**: - 如果在`CheckedChanged`事件中修改状态,需添加标志位防止递归(参考引用[2]的事件处理方案)[^2]。 ```vb Private Sub CheckBox_SCloss_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox_SCloss.CheckedChanged Static flag As Boolean = False If flag Then Exit Sub flag = True ' 这里添加状态修改逻辑 flag = False End Sub ``` #### 常见错误场景验证 - **编译错误测试**:在VB.NET项目中启用`Option Strict On`(推荐设置),尝试`CheckState = True`会立即报错。 - **运行时验证**:使用Try-Catch块捕获异常: ```vb Try CheckBox_SCloss.CheckState = True ' 这将引发InvalidCastException Catch ex As InvalidCastException MessageBox.Show("错误:请使用CheckState.Checked代替True。") End Try ``` #### 附加建议 - **调试工具**:使用Visual Studio的调试器检查`CheckBox_SCloss`的属性和堆栈跟踪。 - **参考最佳实践**:微软官方文档推荐优先使用`Checked`属性进行二元状态管理[^MSDN]。 通过以上步骤,您应该能解决报错问题。如果控件位于复杂容器(如DataGridView),请参考引用[3]的单元格事件处理方案[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值