文档介绍:第4章常用对象
学习内容与要点
使用Convert类
使用String类
使用DateTime结构
使用Color结构
使用Point、Size、Rectangle结构
使用Math类
使用Random类
使用ArryList和Hashtable类
Convert类的使用
用于在不同的基本数据类型之间进行转换。
一些常用方法:
使用 Convert 类将 String 值转换为 Boolean 值
string MyString = "true";
bool MyBool = (MyString);
// MyBool has the value of True.
将字符串转换为数字值
下列代码示例将包含数字字符的字符串转换为 Int32 值:
string newString = "123456789";
int MyInt = (newString);
// MyInt has the value of 123456789
使用 方法的从 Int64 至较小的 Int32 的收缩转换
Int64 MyInt64 = 123456789;
int MyInt = (MyInt64);
// MyInt has the value of 123456789.
将 Double 值转换为 Int32 值
此例中,值从 四舍五入为 43 以完成转换:
Double MyDouble = ;
int MyInt = (MyDouble);
// MyInt has the value of 43
String类
用于对字符串进行各种处理。
1. String类常用属性
Length属性:获取字符串长度。
Chars属性:获取或设置指定位置处的字符。
char a = "test"[1];
string str = "test";
char b = str[1];
Empty属性:表示空字符串
方  法
作  用
Compare
比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等
CompareOrdinal
与Compare一样,但不考虑文化背景
Format
格式化包含各种值的字符串和如何格式化每个值的说明符
IndexOf
定位字符串中第一次出现某个给定子字符串或字符的位置
IndexOfAny
定位字符串中第一次出现某个字符或一组字符的位置
LastIndexOf
与IndexOf一样,但定位最后一次出现的位置
LastIndexOfAny
与IndexOfAny,但定位最后一次出现的位置
PadLeft
在字符串的开头,通过添加指定的重复字符填充字符串
PadRight
在字符串的结尾,通过添加指定的重复字符填充字符串
Replace
用另一个字符或子字符串替换字符串中给定的字符或子字符串
Split
在出现给定字符的地方,把字符串拆分为一个子字符串数组
Substring
在字符串中获取给定位置的子字符串
ToLower
把字符串转换为小写形式
ToUpper
把字符串转换为大写形式
Trim
删除首尾的空白
2. 字符串定位查找
IndexOf方法
格式为:
int IndexOf (char value,int sIndex,int count)
string a = "this is a test";
int b = ("is", 4, 3);
LastIndexOf方法
string a = "this is a test";
int b = ("is", 4, 3);
3.  提取子串
语法格式为:
public string Substring (int startIndex,int length)
string a = "this is a test";
string c = (5, 2);
4. pare和Equals方法
==和!=
string a = "this";
("{0}",("this")); ("{0}",a=="this");
("{0}",pare(a,"this"));