黑马程序员-- .Net学习日记——string类
---------------------- Windows Phone 7手机开发、Net培训、期待与您交流! ----------------------
Null字符串和空字符串
已声明但尚未分配值的字符串为 。 尝试对该字符串调用方法引出NullReferenceException。 空字符串是不同于空的字符串,是其值为“”或StringEmpty 的字符串。 在某些情况下,将 null 字符串或空字符串作为参数传递给方法将引发异常。 例如,把 Null 字符串传递到 Int32Parse 方法将引出ArgumentNullException,传递空字符串将引出FormatException。 在其他情况下,方法参数可以为 null 字符串或空字符串。 例如,要提供类的IFormattable 实现,则需要用常规(“G”)格式指示符使 Null 字符串等同于空字符串。
String 类包括以下两种方便的方法,使您能够测试字符串是否为 或为空:
IsNullOrEmpty,指示字符串是否为 或与StringEmpty相等。 该方法消除了使用如下代码的需要:
If str Is Nothing OrElse str.Equals(String.Empty) Then
if (str ==null || str.Equals(String.Empty))
IsNullOrWhiteSpace,指示字符串是否为 ,等于 StringEmpty,或仅由空白字符组成。 该方法消除了使用如下代码的需要:
If str Is Nothing OrElse str.Equals(String.Empty) OrElse str.Trim().Equals(String.Empty)
if (str ==null || str.Equals(String.Empty) || str.Trim().Equals(String.Empty))
下面的示例使用自定义Temperature 类的IFormattableToString 实现中的IsNullOrEmpty 方法。 该方法支持"G"、"C"、"F"和"K" 格式字符串。 如将空格式字符串或值为 的格式字符串传递到该方法,则其值更改为“G”格式字符串。
Public Overloads Function ToString(fmt As String, provider As IFormatProvider) As String _
Implements IFormattable.ToString
If String.IsNullOrEmpty(fmt) Then fmt = "G"
If provider Is Nothing Then provider = CultureInfo.CurrentCulture
Select Case fmt.ToUpperInvariant()
' Return degrees in Celsius.
Case "G", "C"
Return temp.ToString("F2", provider) + "°C"
' Return degrees in Fahrenheit.
Case "F"
Return (temp * 9 / 5 + 32).ToString("F2", provider) + "°F"
' Return degrees in Kelvin.
Case "K"
Return (temp + 273.15).ToString()
Case Else
Throw New FormatException(
String.Format("The {0} format string is not supported.",
fmt))
End Select
End Function
publicstring ToString(string format, IFormatProvider provider)
{
if(String.IsNullOrEmpty(format)) format = "G";
if(provider == null) provider = CultureInfo.CurrentCulture;
switch(format.ToUpperInvariant())
{
// Return degrees in Celsius.
case"G":
case"C":
returntemp.ToString("F2", provider) + "°C";
// Return degrees in Fahrenheit.
case"F":
return(temp * 9 / 5 + 32).ToString("F2", provider) + "°F";
// Return degrees in Kelvin.
case"K":
return(temp + 273.15).ToString();
default:
thrownewFormatException(
String.Format("The {0} format string is not supported.",
format));
}
}
字符串和索引
索引是 Char 对象在String 中的位置(而不是 Unicode 字符的位置)。 索引是从零开始、从字符串的起始位置(其索引为零)计起的非负数字。 一些搜索方法,例如IndexOf 和LastIndexOf 返回字符串实例中的字符或子字符串的索引。
Chars 属性可让您按其在字符串中的索引位置访问各个Char 对象。 因为Chars 属性为(在 Visual Basic 中的)默认属性或索引器(在 C# 中),所以可使用如下代码在字符串中访问单个Char 对象。 此代码查找字符串中的空白或标点字符,以确定该字符串包含多少字。
Module Example Public Sub Main() Dim s1 As String = "This string consists of a single short sentence." Dim nWords As Integer = 0 s1 = s1.Trim() For ctr As Integer = 0 To s1.Length - 1 If Char.IsPunctuation(s1(ctr)) Or Char.IsWhiteSpace(s1(ctr)) nWords += 1 End If Next Console.WriteLine("The sentence{2} {0}{2}has {1} words.", s1, nWords, vbCrLf) End SubEnd Module' The example displays the following output:' The sentence' This string consists of a single short sentence.' has 8 words.
using System;
public class Example
{
public static void Main()
{
string s1 = "This string consists of a single short sentence.";
int nWords = 0;
s1 = s1.Trim();
for (int ctr =0; ctr < s1.Length; ctr++) {
if (Char.IsPunctuation(s1[ctr]) | Char.IsWhiteSpace(s1[ctr]))
nWords++;
}
Console.WriteLine("The sentence\n {0}\nhas {1} words.",
s1, nWords);
}
}
如下示例所示,因为 String 类实现 IEnumerable 接口,所以还可以使用foreach 构造在字符串中通过 Char 对象进行循环访问。
Module Example
Public Sub Main()
Dim s1 As String = "This string consists of a single short sentence."
Dim nWords As Integer = 0
s1 = s1.Trim()
For Each ch In s1
If Char.IsPunctuation(ch) Or Char.IsWhiteSpace(ch) Then nWords += 1
End If
Next
Console.WriteLine("The sentence{2} {0}{2}has {1} words.", s1, nWords, vbCrLf)
End Sub
End Module
The example displays the following output:
The sentence
This string consists of a single short sentence.
has 8 words.
----------------------Windows Phone 7手机开发、Net培训、期待与您交流! ----------------------
详细请查看:http://net.itheima.com/