Effective C#-Working with Strings (翻译)
来源:互联网 发布:nba2k18球员数据 编辑:程序博客网 时间:2024/05/21 01:52
看到一篇适合初学者的小短文,就翻译了过来,飞刀板砖只管来,做好准备了:) .
Effective C#-Working with Strings
Author: Mahesh Chand
1。空字符串
检查一个字符串是否为空,一个有效的方法是使用string类的Length属性来取代使用null或与""比较。
string str1 = AMethodReturnsString()
{
//Do something and return a string
}
if (str1.Length>0)
{
//do something
}
2。字符串连接
每当修改一个字符串使之返回新的字符串时,建立许多字符串对像会降低你程序的性能,你可以使用 StringBuilder类来避免建立一个新的字符串实例。
你想连接两个字符串,这是通常的方法:
string str1 = "I like ";
string str2 = "Soccer";
string strConcat = string.Concat(str1,str2);
strConcat的结果为"I like Soccer",你可以使用StringBuilder类的Append方法做同样的事情。
StringBuilder MyStrBuilder = new StringBuilder ("I like ");
String newStr = "Soccer";
MyStrBuilder.Append(newStr);
MyStrBuilder的结果也是 I like Soccer.
3。比较字符串
使用String.Equals方法来比较两个字符串。
string str1 = AMethodReturnsString()
if (str1.Equals("TestSting") )
{
// do something
}
- Effective C#-Working with Strings (翻译)
- Effective C#-Working with Strings (翻译)
- Working with Strings(使用Oracle字符串)
- Working with Strings
- Chapter 1: Working with strings
- Python Basics: Working with Strings
- windows编程之二 working with strings
- 《Effective C#》 翻译札记
- 《Effective C#》 翻译札记
- 《Effective C#》 翻译札记
- 《Effective C#》 翻译札记
- 482 C. Game with Strings
- [转贴]《Effective C#》 翻译札记
- 《Effective Modern C++》翻译--简介
- Working with Real-Time Media Streams(翻译自JMF)
- iOS Working with Streams 处理流 官方文档翻译(四)
- [翻译] Effective C++, 3rd Edition, Preface(前言)
- [翻译] Effective C++, 3rd Edition, Introduction(导言)
- The construction of enterprise computer network management information system
- 职场寓言:只有永远的利益 没有永远的朋友
- 在repeater、datalist控件中使用分页功能
- Java程序的国际化和本地化介绍
- 分頁控制
- Effective C#-Working with Strings (翻译)
- C++中++的探讨
- java非对称加密RSA的工具类及其源代码
- DES对称加密的实现及其源代码(原创)
- java中传值和传址及其引伸深度克隆的思考
- OOA/D的统一构建(UP)过程之一:需求分析阶段USE CASE
- OOA/D的统一构建(UP)过程之二:需求分析阶段之Domain model
- thinking C++ 卷2
- thinkng in c++卷2