Features

Strings Are Immutable.

A String Is a Reference Type

Value Type

Store their data directly

Example: int, decimal, bool, enum

Reference Type

Store references to their data

Example: string, object

String Method Best Practices

Do:

Look at what .NET provides before writing your own

Use Intellisense to view the list of available methods

Avoid:

Calling string methods on null strings

Handling Nulls

String.IsNullOrWhiteSpace(cendor)

vendor?.ToLower();

Do:

Write unit tests that cover null conditions

Use IsNullOrWhiteSpace when testing for null in a block of code

Use the null-conditional operator ? when checking for null in a single statement

Verbatim String Literal

var directions = @"Insert \r\n to define a new line";

Insert \r\n to define a new line

Do:

Use verbatim string literals when the string contains special characters such as backslashes.

Use verbatim string literals to hold folder or file names, @"c:\mydir\myfile.txt"

Use two quotes to include quotes in a verbatim string literal, @"Say it with a long ""a"" sound"

Avoid:

Using verbatim string literals when there is no reason, @"Frodo"

String.Format

Do:

Use String.Format to insert the value of an expression into a string

Include a formatting string as needed, like String.Format("Deliver by:{0:d}", deliveryBy))

Avoid:

Use String.Format when concatenating string literals, like String.Format("Hello {0}", "World");

String Interpolation

var pc = String.Format("{0}-{1}", product.Category, product.SequenceNumber);

var pc = $"{product.Category}-{product.SequenceNumber}";

StringBuilder

Conceptually a mutable string

Allow string operations, such as concatenation, without creating a new string

Provides methods for manipulating the mutable string - Append, Insert, Replace, etc

Use ToString to convert a string

More efficient when working with strings that are

- Build up with many separate concatenation operations

- Changed a large number of times, such as within a loop

Do:

Use StringBuilder when building up a string with numerous concatenation operations

Use StringBuilder when modifying a string numerous times (such as a loop)

Consider readability

Avoid:

Use StringBuilder when only modify a string a few times

FAQ

1.What does it mean to say that C# strings are immutable?

It means that strings cannot be modified once they are created.

2.Is a string a value type or a reference type?

A string is a reference type, but acts like a value type.

3.What is the best way to check for null string?

Use String.IsNullOrWhiteSpace is great when checking nulls for a code block

Use the new C# 6 null-conditional operator ? is great for code statements

4.What are the benefits to using StringBuilder?

The .NET StringBuilder class is mutable, meaning that it can be readily changed.

Using StringBuilder is therefore more efficient when appending lots of strings.

C# Best Practices - Handling Strings的更多相关文章

  1. [翻译] GONMarkupParser

    GONMarkupParser https://github.com/nicolasgoutaland/GONMarkupParser NSString *inputText = @"Sim ...

  2. str_replace vs preg_replace

    转自:http://benchmarks.ro/2011/02/str_replace-vs-preg_replace/ 事实证明str_replace确实比preg_replace快. If you ...

  3. MySQL 中的 FOUND_ROWS() 与 ROW_COUNT() 函数

    移植sql server 的存储过程到mysql中,遇到了sql server中的: IF @@ROWCOUNT < 1 对应到mysql中可以使用 FOUND_ROWS() 函数来替换. 1. ...

  4. nodejs(三)Buffer module & Byte Order

    一.目录 ➤ Understanding why you need buffers in Node ➤ Creating a buffer from a string ➤ Converting a b ...

  5. Zend API:深入 PHP 内核

    Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" ...

  6. Java实现LeetCode_0020_ValidParentheses

    package javaLeetCode.primary; import java.util.Scanner; import java.util.Stack; public class ValidPa ...

  7. OSCP Learning Notes - Privilege Escalation

    Privilege Escalation Download the Basic-pentesting vitualmation from the following website: https:// ...

  8. Java – Top 5 Exception Handling Coding Practices to Avoid

    This article represents top 5 coding practices related with Java exception handling that you may wan ...

  9. Exception (3) Java exception handling best practices

    List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...

随机推荐

  1. 求模和求余(附加C语言实现)

    求模和求余的总体计算步骤如下: 1.求整数商  c = a/b 2.计算模或者余数 r = a - c*b 求模和求余的第一步不同,求余在取c的值时向0方向舍入;取模在计算c的值时向无穷小方向舍入. ...

  2. HTML标签解释大全

      一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD).   标签:a 说明:标明超链接的起始或目的位置.   标签:acronym 说明:标明缩写词. ...

  3. css 自适应布局阮一峰

    转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...

  4. MVC模式和URL访问

    一.什么是MVC //了解 M -Model 编写model类 对数据进行操作 使用Model类 来操作数据 V -View 编写html文件,页面呈现 C -Controller 编写类文件(Use ...

  5. date命令使用

    date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...

  6. iOS 首次启动画面,新装或更新用户可以通过它查看简介。

    // // GuideViewController.h // Guide // // Created by twb on 13-9-17. // Copyright (c) 2013年 twb. Al ...

  7. 《Linux命令行与shell脚本编程大全》 第十五章 学习笔记

    第十五章:控制脚本 处理信号 重温Linux信号 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能 ...

  8. ural 1057(数位dp)

    数位dp题,关键是用树的思维去考虑. 对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0. 具体读者可以去参考,国家集训队李聪的论文,里 ...

  9. hibernate-search-5.1.1简易使用

    系统要求java7和hibernate-core-4.3.8,此外还依赖如下jar包 使用demo如下: package com.ciaos.controller; import java.io.IO ...

  10. java实现 阿拉伯数字转换为汉字数字 算法

    package test; public class NumberFormatTest { static String[] units = { "", "十", ...