The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. This makes testing for string equality more intuitive.
(C#基础) 字符串数据
字符串是 String 类型的对象,它的值是文本。在内部,文本被存储为 Char 对象的顺序只读集合。 C# 字符串末尾没有以 null 结尾的字符;因此 C# 字符串可以包含任意数目的嵌入式 null 字符(“\0”)。 字符串的 Length 属性代表它包含的 Char 对象的数量,而不是 Unicode 字符的数量。 若要访问字符串中的各个 Unicode 码位,请使用StringInfo 对象。
System.String 程序集提供了很多方法,常用的如下:
字符串成员 分类 作用
Length 属性 返回当前字符串的长度
Compare() 方法 比较两个字符串
Contains() 方法 判定当前字符串中是否办好一个指定的子字符串
Equals() 方法 测试两个字符串对象是否含有同样的字符数据
Format() 方法 静态方法,利用数据基本类型和{0}符号格式化一个字符串
Insert() 方法 将一个字符串插入到给定字符串中
PadLeft() 方法 在字符串内填充字符
PadRight() 方法 在字符串内填充字符
Remove() 方法
Replace() 方法
Splite()
Trim()
ToUpper()
ToLower()
IndexOf() 方法 查找子字符(串) 在字符串中第一次出现的位置(Index是从0开始,如果返回值为-1,表示子串不存在)
LastIndexOf() 方法 报告指定的 String 在此实例内的最后一个匹配项的索引位置。
Substring() 方法 截取指定条件(起始位置,长度等) 的子字符串。 重载方法: Substring(int32) , Substring(int32, int32)
名称 | 说明 |
---|---|
String.Substring (Int32) | 从此实例检索子字符串。子字符串从指定的字符位置开始。
由 .NET Compact Framework 支持。 |
String.Substring (Int32, Int32) | 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。
由 .NET Compact Framework 支持。 |
字符串操作:
string[] 数组和 list<string>泛型 相互转化
// string[] 转化为 list<string> string[] str = {"Say","Hello"}; list<string> list = new list<string>(str); // list<string> 转化为 string[] list<string> list = new list<string>; string[] st r= list.toarray();
字符串截取:
2.1)题:从这个字符串“Device Name: Generic USB Hub Device Hardware IDs: USB\VID_8087&PID_0024&REV_0000 Device Class: USB Serial Number= Device Status: 0” 中依次取出“Generic USB Hub”,“USB\VID_8087&PID_0024&REV_0000”,“USB”,“”,"0"给相应的结构体ComponentInfo.
思路:使用IndexOf()方法来获取子字符串的起止位置,再用Substring()方法来取出字符串。
Int 数组转化为String
例如 int[] arr = new int[]{1,2,3,4,5}; 需要转化为 string 为:"1,2,3,4,5".
var result =string.Join(",", arr); This uses the following overload of string.Join: publicstaticstringJoin<T>(string separator,IEnumerable<T> values);
如何输出大括号{}.
For you to output foo {1, 2, 3} you have to do something like:
string t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);
To output a { you use {{ and to output a } you use }}.
"{{" is treated as the escaped bracket character in a format string.
@ 逐字符
Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:
@"good morning" // a string literal
The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
To include a double quotation mark in an @-quoted string, double it:
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
Another use of the @ symbol is to use referenced (/reference) identifiers that are C# keywords.
For more information about strings in C#, see Strings (C# Programming Guide).
参考:http://msdn.microsoft.com/zh-cn/library/vstudio/ms228362.aspx