惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
罗磊的独立博客
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX

博客园 - bengxia

DataGridView中的{索引-1沒有值}异常 Silverlight + WCF + Sharepoint(Silverlight Web Part) 问题集合 CLR Via C# 3rd 阅读摘要 -- Chapter 29 – Hybrid Thread Synchronization Constructs CLR Via C# 3rd 阅读摘要 -- Chapter 28 – Primitive Thread Synchronization Constructs FIX: Unable to find a version of the runtime to run this application. CLR Via C# 3rd 阅读摘要 -- Chapter 27 – I/O-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 26 – Compute-Bound Asynchronous Operations CLR Via C# 3rd 阅读摘要 -- Chapter 25 – Thread Basics CLR Via C# 3rd 阅读摘要 -- Chapter 24 – Runtime Serialization CLR Via C# 3rd 阅读摘要 -- Chapter 23 – Assembly Loading and Reflection CLR Via C# 3rd 阅读摘要 -- Chapter 22 – CLR Hosting and AppDomains CLR Via C# 3rd 阅读摘要 -- Chapter 21 – Automatic Memory Management (Garbage Collection) CLR Via C# 3rd 阅读摘要 -- Chapter 20 – Exceptions and State Management - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 19 – Nullable Value Types CLR Via C# 3rd 阅读摘要 -- Chapter 18 - Custom Attributes CLR Via C# 3rd 阅读摘要 -- Chapter 17 - Delegates CLR Via C# 3rd 阅读摘要 -- Chapter 16 - Arrays - bengxia CLR Via C# 3rd 阅读摘要 -- Chapter 15 - Enumerated Types and Bit Flags CLR Via C# 3rd 阅读摘要 -- Chapter 13 - Interfaces
CLR Via C# 3rd 阅读摘要 -- Chapter 14 - Chars, Strings, a...
bengxia · 2010-04-13 · via 博客园 - bengxia

Characters

1.  System.Char,表示16位的Unicode代码;

2. GetUnicodeCategory()获得System.Globalization.UnicodeCategory枚举;

3. IsDigit, IsLetter, IsWhiteSpace, IsUpper, IsLower, IsPunctuation, IsLetterOrDigit, IsControl, IsNumber, IsSeparator, IsSurrogate, IsLowSurrogate, IsHighSurrogate, IsSymbol;

4. System.Threading.Thread.CurrentCulture;

5. Char.GetNumericValue('\u0033');

6. 数值类型与Char实例互相转换的方法:

  • casting,最快;
  • 使用Convert类型,较快; 
  • 使用IConvertible接口,最慢。 

The System.String Type

1. 不能使用new来构造一个String对象,比如String s = new String("abc")是错误的;

2. 推荐使用System.Environment.NewLine等等来代替硬编码的转义字符(\r\n);

3. 无转义的字符串前导符@,String file = @"C:\abc.txt",这样反斜杠就不会作为转义字符; 

4. String是不可改变的类型,从性能的角度出发,如果需要频繁更改String的值,那么用StringBuilder;

5. StringComparsion.InvariantCulture和InvariantCultureIgnoreCase一般不用;

6. String.ToUpperInvariant比ToLowerInvariant要高效,不使用ToUpper和ToLower是因为这两个方法是文化敏感的; 

7. 默认情况下,CompareTo是文化敏感的比较,Equals是顺序的比较; 

8. System.Globalization.CultureInfo的两个属性:CurrentUICulture、CurrentCulture;

9. System.StringComparer很有用;

10. String Interning:首先在内部的散列表中找这个字符串,如果有直接返回散列表中的这个引用,如果没有,加入再返回。别滥用;

11. String Pooling:合并多次出现的字符串到一个单独的实例,消减模块大小;

12. System.Globalization.StringInfo;

13. 其他的String方法:Clone, Copy, CopyTo, Substring, ToString; Insert, Remove, PadLeft, Replace, Split, Join, ToLower, ToUpper, Trim, Concat, Format。

Constructing a String Efficiently

1. System.Text.StringBuilder,没必要再强调了。

Obtaining a String Representation of an Object:ToString

1. System.Object的ToString简单的返回对象的类型全名;

2. 无参数的ToString方法有两个问题:

  • 调用者不能控制字符串的格式;
  • 调用者不能简单的选择字符串的特定文化。 

3.  实现System.IFormatable接口,参考Formatting Types;

4. FLC中有三种类型实现了IFormatProvider接口:

  • CultureInfo;
  • NumberFormatInfo;
  • DateTimeFormatInfo。

5. 格式多个对象到一个单独的字符串:String s = String.Format("{0} {1} {2} ...", a, b, c);

6. StringBuilder.AppendFormat。

Parsing a String to Obtain an Object:Parse

1. DateTime.ParseExact,参考DateTimeFormatInfo;

2. TryParse,性能比Parse稍微差一点。 

Encoding:Converting Between Characters and Bytes

1. Unicode:UTF-8,UTF-16, UTF-32, UTF-7, ASCII,除非自己真的清楚,那么总是使用UTF-8或者UTF-16;

2. System.Text.Encoding.Encoding,System.Text.Decoder,BitConverter.ToString();

3. 现在我们常用的CPU体系结构的一般都是LittleEndian的;

4. Base64编码解码。

Secure Strings

1. System.Security.SecureString,当该对象构造时,会分配一块非托管内存用来放置字符数组;

2. 决不要把SecureString中的内容放到String中;

3. 使用SecureString时,需要采用/unsafe编译;

4. Marshal.SecureStringToCoTaskMemUnicode()、ZeroFreeCoTaskMemUnicode()方法。 

本章小结

 本章解释了在.Net中使用字符和字符串的工作机制。开始讨论了System.Char结构以及操作一个字符的不同方法,然后讲了System.String类型,注意String是不可变的,在什么情况下使用StringBuilder。在这些基础之上,还讲述了如何格式化对象ToString,以及如何高效可靠的在不同编码之间进行转换。最后演示了如何使用SecureString来安全的存储密码、信用卡等敏感信息。