(C#基础) 字符串数据和操作
2013-04-16 18:54
381 查看
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,表示子串不存在)
Substring() 方法 截取指定条件(起始位置,长度等) 的子字符串。 重载方法: Substring(int32) , Substring(int32, int32)
字符串操作:
string[] 数组和 list<string>泛型 相互转化
字符串截取:
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".
如何输出大括号{}.
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:
C#
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#
To include a double quotation mark in an @-quoted string, double it:
C#
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
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,表示子串不存在)
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:
C#
@"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#
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
To include a double quotation mark in an @-quoted string, double it:
C#
@"""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
相关文章推荐
- 利用Python进行数据分析(15) pandas基础: 字符串操作
- C#基础系列(3)-- 第一部分 基础数据类型与操作 -- 位运算(3)
- C#基础之操作字符串的方法
- 0基础学C#教程1--------comboBox的相关操作(包括下拉,多数据操作等等)
- C# 两个ListBox 数据互传-基础操作
- 利用Python进行数据分析(15) pandas基础: 字符串操作
- C#基础系列(5)-- 第一部分 基础数据类型与操作 -- 反码操作符(5)
- c#基础数据操作之遍历DataTable并输出
- C#基础系列(1)-- 第一部分 基础数据类型与操作 -- 位运算(1)
- C#基础系列(2)-- 第一部分 基础数据类型与操作 -- 位运算(2)
- C# 操作JSON字符串用于与网页中的返回数据进行互动
- C#基础系列(4)-- 第一部分 基础数据类型与操作 -- 枚举类型与位标志(4)
- [C#基础知识系列]专题六:C#字符串操作总结
- C# 基础 字符串 集合 文件操作
- C#基础学习之【4】字符串的操作
- C# 数据表(Dataset)操作 合并 查询一箩筐
- 数据结构基础之数组实现线性表各种操作(二)
- C#读写内置类型的数据时是否原子操作
- C#.net 数据操作--excel 操作类
- REP与STOS串操作指令相结合使用怎么完成建立一组相同数据的字符串?