SlideShare a Scribd company logo
1 of 40
STRING IN .NET
Larry Nung
AGENDA
String
String operations
“”& String.Empty
Null & Empty check
String pool
String property
StringBuilder
Reference
Q & A
2
STRING
3
STRING
[SerializableAttribute] [ComVisibleAttribute(true)]
public sealed class String : IComparable,
ICloneable, IConvertible, IComparable<string>,
IEnumerable<char>, IEnumerable,
IEquatable<string>
4
STRING
[SerializableAttribute] [ComVisibleAttribute(true)]
public sealed class String : IComparable,
ICloneable, IConvertible, IComparable<string>,
IEnumerable<char>, IEnumerable,
IEquatable<string>
5
STRING
6
STRING
7
STRING OPERATIONS
8
STRING OPERATIONS
var name = "LevelUp";
var url = "http://larrynung.github.io/";
var str1 = name + " (" + url + ")";
var str2 = string.Concat(name, " (", url, ")");
var str3 = string.Format("{0} ({1})", name, url);
var str4 = @"Blog: LevelUp Url: http://larrynung.github.io/";
var str5 = "c:BlogLevelUp";
var str6 = @"c:BlogLevelUp";
var msg = string.Empty;
msg += String.Format("str1 == str2 => {0}", str1 == str2);
msg += Environment.NewLine;
msg += String.Format("str1.Equals(str3) => {0}", str1.Equals(str3));
msg += Environment.NewLine;
msg += str4;
msg += Environment.NewLine;
msg += str5;
msg += Environment.NewLine;
msg += str6;
msg += Environment.NewLine; Console.WriteLine(msg);
9
“”& STRING.EMPTY
10
“” & STRING.EMPTY
11
“” & STRING.EMPTY
static void Main(string[] args) {
int count = 1000000000;
EmptyString1(count); EmptyString2(count); EmptyString3(count);
}
private static void EmptyString1(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = ""; }
sw.Stop();
Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString2(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = string.Empty; }
sw.Stop();
Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString3(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); }
sw.Stop();
Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString());
}
12
“” & STRING.EMPTY
static void Main(string[] args) {
int count = 1000000000;
EmptyString1(count); EmptyString2(count); EmptyString3(count);
}
private static void EmptyString1(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = ""; }
sw.Stop();
Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString2(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = string.Empty; }
sw.Stop();
Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString3(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); }
sw.Stop();
Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString());
}
130
1000
2000
3000
4000
5000
6000
7000
1000000 10000000 1000000000
""
String.Empty
new String()
“” & STRING.EMPTY
static void Main(string[] args) {
int count = 1000000000;
EmptyString1(count); EmptyString2(count); EmptyString3(count);
}
private static void EmptyString1(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = ""; }
sw.Stop();
Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString2(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = string.Empty; }
sw.Stop();
Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString());
}
private static void EmptyString3(int count) {
String test; Stopwatch sw = Stopwatch.StartNew();
for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); }
sw.Stop();
Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString());
}
140
1000
2000
3000
4000
5000
6000
7000
1000000 10000000 1000000000
""
String.Empty
new String()
NULL & EMPTY CHECK
15
NULL & EMPTY CHECK
 Check with == null
 e.x. if (str == nill)
 Check with == ""
 e.x. if (str == “”)
 Check with == String.Empty
 e.x. if (str == String.Empty)
 Check with String.IsNullOrEmpty(str)
 e.x. if (String.IsNullOrEmpty(str))
 Check with String.Length == 0
 e.x. if (str.Length == 0)
16
NULL & EMPTY CHECK
private static void Main(string[] args) {
string[] testStrings = new string[] { null, "", string.Empty };
foreach (string str in testStrings) {
TestString(str);
Console.WriteLine("=======================");
}
}
private static void TestString(string str) {
if (str == null)
Console.WriteLine("str = null");
if (str == "")
Console.WriteLine("str = """);
if (str == string.Empty)
Console.WriteLine("str = String.Empty");
if (string.IsNullOrEmpty(str))
Console.WriteLine("String.IsNullOrEmpty(str)");
try {
if (str.Length == 0) Console.WriteLine("str.Length = 0");
} catch (Exception ex) { }
}
17
NULL & EMPTY CHECK
private static void Main(string[] args) {
string[] testStrings = new string[] { null, "", string.Empty };
foreach (string str in testStrings) {
TestString(str);
Console.WriteLine("=======================");
}
}
private static void TestString(string str) {
var count = 100000000;
Console.WriteLine("str == null: {0} ms",
DoTest(count, () => { var result = str == null; }));
Console.WriteLine("str == "": {0} ms",
DoTest(count, () => { var result = str == ""; }));
Console.WriteLine("str == String.Empty: {0} ms",
DoTest(count, () => { var result = str == String.Empty; }));
Console.WriteLine("string.IsNullOrEmpty(str): {0} ms",
DoTest(count, () => { var result = string.IsNullOrEmpty(str); }));
try {
Console.WriteLine("str.Length == 0: {0} ms",
DoTest(count, () => { var result = str.Length == 0; }));
} catch { }
}
static long DoTest(int count, Action action)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
18
NULL & EMPTY CHECK
private static void Main(string[] args) {
string[] testStrings = new string[] { null, "", string.Empty };
foreach (string str in testStrings) {
TestString(str);
Console.WriteLine("=======================");
}
}
private static void TestString(string str) {
var count = 100000000;
Console.WriteLine("str == null: {0} ms",
DoTest(count, () => { var result = str == null; }));
Console.WriteLine("str == "": {0} ms",
DoTest(count, () => { var result = str == ""; }));
Console.WriteLine("str == String.Empty: {0} ms",
DoTest(count, () => { var result = str == String.Empty; }));
Console.WriteLine("string.IsNullOrEmpty(str): {0} ms",
DoTest(count, () => { var result = string.IsNullOrEmpty(str); }));
try {
Console.WriteLine("str.Length == 0: {0} ms",
DoTest(count, () => { var result = str.Length == 0; }));
} catch { }
}
static long DoTest(int count, Action action)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i) action();
return sw.ElapsedMilliseconds;
}
19
STRING POOL
20
STRING POOL
var str1 = "aaaa";
var str2 = "aa" + "aa";
var str3 = "aa"; str3 += str3;
var str4 = new string('a', 4);
var str5 = string.Intern(str4);
Debug.WriteLine(ReferenceEquals(str1, str2));
Debug.WriteLine(ReferenceEquals(str1, str3));
Debug.WriteLine(ReferenceEquals(str1, str4));
Debug.WriteLine(ReferenceEquals(str1, str5));
21
STRING POOL
var str1 = "aaaa";
var str2 = "aa" + "aa";
var str3 = "aa"; str3 += str3;
var str4 = new string('a', 4);
var str5 = string.Intern(str4);
Debug.WriteLine(ReferenceEquals(str1, str2));
Debug.WriteLine(ReferenceEquals(str1, str3));
Debug.WriteLine(ReferenceEquals(str1, str4));
Debug.WriteLine(ReferenceEquals(str1, str5));
22
STRING POOL
var str1 = "aaaa";
var str2 = "aa" + "aa";
var str3 = "aa"; str3 += str3;
var str4 = new string('a', 4);
var str5 = string.Intern(str4);
Debug.WriteLine(ReferenceEquals(str1, str2));
Debug.WriteLine(ReferenceEquals(str1, str3));
Debug.WriteLine(ReferenceEquals(str1, str4));
Debug.WriteLine(ReferenceEquals(str1, str5));
23
str1
str2
str3
str4
“aaaa”
“aaaa”
str5 “aaaa”
String Pool
STRING PROPERTY
24
STRING PROPERTY
internal class Program
{
public String Name { get; set; }
}
25
STRING PROPERTY
internal class Program
{
public String Name { get; set; }
}
26
STRING PROPERTY
internal class Program
{
public String Name { get; set; }
}
27
STRING PROPERTY
internal class Program
{
private String _name;
public String Name
{
get { return _name ?? String.Empty; }
set { _name = value; }
}
}
28
STRINGBUILDER
29
STRINGBUILDER
 Consider using the String class under these conditions:
 When the number of changes that your app will make to a
string is small. In these cases, StringBuilder might offer
negligible or no performance improvement over String.
 When you are performing a fixed number of concatenation
operations, particularly with string literals. In this case, the
compiler might combine the concatenation operations into a
single operation.
 When you have to perform extensive search operations while
you are building your string. The StringBuilder class lacks
search methods such as IndexOf or StartsWith. You'll have to
convert the StringBuilderobject to a String for these
operations, and this can negate the performance benefit from
using StringBuilder. For more information, see the Searching
the text in a StringBuilder object section.
30
STRINGBUILDER
 Consider using the StringBuilder class under these
conditions:
 When you expect your app to make an unknown
number of changes to a string at design time (for
example, when you are using a loop to concatenate a
random number of strings that contain user input).
 When you expect your app to make a significant
number of changes to a string.
31
STRINGBUILDER
var count = 1000000;
var result = string.Empty;
var sw = Stopwatch.StartNew();
for (var index = 0; index < count; ++index)
result += "a";
Console.WriteLine(sw.ElapsedMilliseconds);
result = string.Empty;
sw.Restart();
var sb = new StringBuilder();
for (var index = 0; index < count; ++index)
sb.Append("a");
result = sb.ToString();
Console.WriteLine(sw.ElapsedMilliseconds);
32
0
50000
100000
150000
200000
250000
300000
350000
1000 10000 100000 1000000
String concat
StringBuilder
“aaaa”
164
“aaaaaaaaaaaaaaaa”
16
“aaaaaaaaaaaaaaaaa”
3217
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
6433
REFERENCE
33
REFERENCE
 [.Net Concept]理解並善用String pool - Level Up- 點
部落
 http://www.dotblogs.com.tw/larrynung/archive/2011/06/3
0/30763.aspx
 [Performance][C#]String.Empty V.S “” - Level Up-
點部落
 http://www.dotblogs.com.tw/larrynung/archive/2009/12/2
2/12615.aspx
 [Performance][VB.NET].NET空字串判斷徹底研究 -
Level Up- 點部落
 http://www.dotblogs.com.tw/larrynung/archive/2009/03/1
3/7461.aspx 34
REFERENCE
 [.NET Concept]集合與字串類型的屬性和方法應避免
回傳null - Level Up- 點部落
 http://www.dotblogs.com.tw/larrynung/archive/2011/04/1
6/22866.aspx
 String與Stringbuilder組字串的效能比較 - Jeff 隨手記
- 點部落
 http://www.dotblogs.com.tw/jeff-
yeh/archive/2008/11/04/5870.aspx
 [C#.NET] 動態處理字串 - StringBuilder 類別 與
String 類別的效能 - 余小章 @ 大內殿堂- 點部落
 http://www.dotblogs.com.tw/yc421206/archive/2010/10/
26/18575.aspx 35
REFERENCE
 StringBuilder串接字串的迷思 - 黑暗執行緒
 http://blog.darkthread.net/blogs/darkthreadtw/archive/20
07/12/15/stringbuilder-for-static-string-concate.aspx
 Heikniemi Hardcoded » .net String vs.
StringBuilder – concatenation performance
 http://www.heikniemi.net/hardcoded/2004/08/net-string-
vs-stringbuilder-concatenation-performance/
 StringBuilder Class (System.Text)
 https://msdn.microsoft.com/en-
us//library/system.text.stringbuilder.aspx
36
REFERENCE
 文章-StringBuilder與String的字串相接效能大車拼 -
黑暗執行緒
 http://blog.darkthread.net/blogs/darkthreadtw/archive/20
09/09/07/article-stringbuilder-vs-string.aspx
 Performance considerations for strings in C# -
CodeProject
 http://www.codeproject.com/Articles/10318/Performance
-considerations-for-strings-in-C
 Improving String Handling Performance in .NET
Framework Applications
 https://msdn.microsoft.com/en-
us/library/aa302329.aspx?f=255&MSPPError=-
2147217396
37
REFERENCE
 Introduction to Programming with C# / Java Books
» Chapter 13. Strings and Text Processing
 http://www.introprogramming.info/english-intro-csharp-
book/read-online/chapter-13-strings-and-text-
processing/
38
Q&A
39
QUESTION & ANSWER
40

More Related Content

What's hot

Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsAlexander Granin
 
D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular500Tech
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftGiordano Scalzo
 
JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?PROIDEA
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012aleks-f
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objectsHusain Dalal
 
[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)croquiscom
 
The Ring programming language version 1.6 book - Part 36 of 189
The Ring programming language version 1.6 book - Part 36 of 189The Ring programming language version 1.6 book - Part 36 of 189
The Ring programming language version 1.6 book - Part 36 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84Mahmoud Samir Fayed
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptLoïc Knuchel
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science Chucheng Hsieh
 
JAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseJAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseSOAT
 

What's hot (20)

D3.js workshop
D3.js workshopD3.js workshop
D3.js workshop
 
Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonads
 
D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 
How to Clone Flappy Bird in Swift
How to Clone Flappy Bird in SwiftHow to Clone Flappy Bird in Swift
How to Clone Flappy Bird in Swift
 
JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?JDD 2016 - Pawel Byszewski - Kotlin, why?
JDD 2016 - Pawel Byszewski - Kotlin, why?
 
Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012Dynamic C++ Silicon Valley Code Camp 2012
Dynamic C++ Silicon Valley Code Camp 2012
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
 
[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)
 
The Ring programming language version 1.6 book - Part 36 of 189
The Ring programming language version 1.6 book - Part 36 of 189The Ring programming language version 1.6 book - Part 36 of 189
The Ring programming language version 1.6 book - Part 36 of 189
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScript
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
Modern technologies in data science
Modern technologies in data science Modern technologies in data science
Modern technologies in data science
 
JAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entrepriseJAVA 8 : Migration et enjeux stratégiques en entreprise
JAVA 8 : Migration et enjeux stratégiques en entreprise
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 

Viewers also liked

Debug windows in visual studio
Debug windows in visual studioDebug windows in visual studio
Debug windows in visual studioLarry Nung
 
Compelling audio and video for metro style games
Compelling audio and video for metro style gamesCompelling audio and video for metro style games
Compelling audio and video for metro style gamesLarry Nung
 
Common.logging
Common.loggingCommon.logging
Common.loggingLarry Nung
 
StackExchange.redis
StackExchange.redisStackExchange.redis
StackExchange.redisLarry Nung
 
GRUNT - The JavaScript Task Runner
GRUNT - The JavaScript Task RunnerGRUNT - The JavaScript Task Runner
GRUNT - The JavaScript Task RunnerLarry Nung
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Larry Nung
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Larry Nung
 
Visual studio 2013
Visual studio 2013Visual studio 2013
Visual studio 2013Larry Nung
 
Regular expression
Regular expressionRegular expression
Regular expressionLarry Nung
 
PL/SQL Coding Guidelines - Part 1
PL/SQL Coding Guidelines - Part 1PL/SQL Coding Guidelines - Part 1
PL/SQL Coding Guidelines - Part 1Larry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5Larry Nung
 
Visual studio 2015
Visual studio 2015Visual studio 2015
Visual studio 2015Larry Nung
 
PL/SQL Coding Guidelines - Part 3
PL/SQL Coding Guidelines - Part 3PL/SQL Coding Guidelines - Part 3
PL/SQL Coding Guidelines - Part 3Larry Nung
 

Viewers also liked (18)

Code contract
Code contractCode contract
Code contract
 
Debug windows in visual studio
Debug windows in visual studioDebug windows in visual studio
Debug windows in visual studio
 
Compelling audio and video for metro style games
Compelling audio and video for metro style gamesCompelling audio and video for metro style games
Compelling audio and video for metro style games
 
Typescript
TypescriptTypescript
Typescript
 
Common.logging
Common.loggingCommon.logging
Common.logging
 
StackExchange.redis
StackExchange.redisStackExchange.redis
StackExchange.redis
 
Specflow
SpecflowSpecflow
Specflow
 
GRUNT - The JavaScript Task Runner
GRUNT - The JavaScript Task RunnerGRUNT - The JavaScript Task Runner
GRUNT - The JavaScript Task Runner
 
Disruptor
DisruptorDisruptor
Disruptor
 
Garbage collection in .net (basic level)
Garbage collection in .net (basic level)Garbage collection in .net (basic level)
Garbage collection in .net (basic level)
 
Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...Topshelf - An easy service hosting framework for building Windows services us...
Topshelf - An easy service hosting framework for building Windows services us...
 
Visual studio 2013
Visual studio 2013Visual studio 2013
Visual studio 2013
 
Regular expression
Regular expressionRegular expression
Regular expression
 
PL/SQL Coding Guidelines - Part 1
PL/SQL Coding Guidelines - Part 1PL/SQL Coding Guidelines - Part 1
PL/SQL Coding Guidelines - Part 1
 
PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5PL/SQL & SQL CODING GUIDELINES – Part 5
PL/SQL & SQL CODING GUIDELINES – Part 5
 
Visual studio 2015
Visual studio 2015Visual studio 2015
Visual studio 2015
 
PL/SQL Coding Guidelines - Part 3
PL/SQL Coding Guidelines - Part 3PL/SQL Coding Guidelines - Part 3
PL/SQL Coding Guidelines - Part 3
 
C# 6.0
C# 6.0C# 6.0
C# 6.0
 

Similar to String in .net

13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text ProcessingIntro C# Book
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010RonnBlack
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02Abdul Samee
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationJoni
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation xShahjahan Samoon
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019corehard_by
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoThe Software House
 
C# Starter L04-Collections
C# Starter L04-CollectionsC# Starter L04-Collections
C# Starter L04-CollectionsMohammad Shaker
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88Mahmoud Samir Fayed
 
Text processing
Text processingText processing
Text processingIcancode
 

Similar to String in .net (20)

13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
Unitii string
Unitii stringUnitii string
Unitii string
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET Application
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
The Art of Clean Code
The Art of Clean CodeThe Art of Clean Code
The Art of Clean Code
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
C# Starter L04-Collections
C# Starter L04-CollectionsC# Starter L04-Collections
C# Starter L04-Collections
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Java programs
Java programsJava programs
Java programs
 
The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88The Ring programming language version 1.3 book - Part 25 of 88
The Ring programming language version 1.3 book - Part 25 of 88
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
Text processing
Text processingText processing
Text processing
 

More from Larry Nung

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automationLarry Nung
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the webLarry Nung
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8Larry Nung
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7Larry Nung
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarkingLarry Nung
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6Larry Nung
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualityLarry Nung
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017Larry Nung
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command lineLarry Nung
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETLarry Nung
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4Larry Nung
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configurationLarry Nung
 
Bower - A package manager for the web
Bower - A package manager for the webBower - A package manager for the web
Bower - A package manager for the webLarry Nung
 
Generic lazy class
Generic lazy classGeneric lazy class
Generic lazy classLarry Nung
 
PL/SQL Coding Guidelines - Part 2
PL/SQL Coding Guidelines - Part 2PL/SQL Coding Guidelines - Part 2
PL/SQL Coding Guidelines - Part 2Larry Nung
 

More from Larry Nung (19)

Ansible - simple it automation
Ansible - simple it automationAnsible - simple it automation
Ansible - simple it automation
 
sonarwhal - a linting tool for the web
sonarwhal - a linting tool for the websonarwhal - a linting tool for the web
sonarwhal - a linting tool for the web
 
LiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data fileLiteDB - A .NET NoSQL Document Store in a single data file
LiteDB - A .NET NoSQL Document Store in a single data file
 
PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8PL/SQL & SQL CODING GUIDELINES – Part 8
PL/SQL & SQL CODING GUIDELINES – Part 8
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7PL/SQL & SQL CODING GUIDELINES – Part 7
PL/SQL & SQL CODING GUIDELINES – Part 7
 
BenchmarkDotNet - Powerful .NET library for benchmarking
BenchmarkDotNet  - Powerful .NET library for benchmarkingBenchmarkDotNet  - Powerful .NET library for benchmarking
BenchmarkDotNet - Powerful .NET library for benchmarking
 
PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6PLSQL Coding Guidelines - Part 6
PLSQL Coding Guidelines - Part 6
 
SonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code QualitySonarQube - The leading platform for Continuous Code Quality
SonarQube - The leading platform for Continuous Code Quality
 
Visual studio 2017
Visual studio 2017Visual studio 2017
Visual studio 2017
 
Web deploy command line
Web deploy command lineWeb deploy command line
Web deploy command line
 
Web deploy
Web deployWeb deploy
Web deploy
 
SikuliX
SikuliXSikuliX
SikuliX
 
protobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NETprotobuf-net - Protocol Buffers library for idiomatic .NET
protobuf-net - Protocol Buffers library for idiomatic .NET
 
PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4PL/SQL & SQL CODING GUIDELINES – Part 4
PL/SQL & SQL CODING GUIDELINES – Part 4
 
Fx.configuration
Fx.configurationFx.configuration
Fx.configuration
 
Bower - A package manager for the web
Bower - A package manager for the webBower - A package manager for the web
Bower - A package manager for the web
 
Generic lazy class
Generic lazy classGeneric lazy class
Generic lazy class
 
PL/SQL Coding Guidelines - Part 2
PL/SQL Coding Guidelines - Part 2PL/SQL Coding Guidelines - Part 2
PL/SQL Coding Guidelines - Part 2
 

String in .net

  • 2. AGENDA String String operations “”& String.Empty Null & Empty check String pool String property StringBuilder Reference Q & A 2
  • 4. STRING [SerializableAttribute] [ComVisibleAttribute(true)] public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string> 4
  • 5. STRING [SerializableAttribute] [ComVisibleAttribute(true)] public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string> 5
  • 9. STRING OPERATIONS var name = "LevelUp"; var url = "http://larrynung.github.io/"; var str1 = name + " (" + url + ")"; var str2 = string.Concat(name, " (", url, ")"); var str3 = string.Format("{0} ({1})", name, url); var str4 = @"Blog: LevelUp Url: http://larrynung.github.io/"; var str5 = "c:BlogLevelUp"; var str6 = @"c:BlogLevelUp"; var msg = string.Empty; msg += String.Format("str1 == str2 => {0}", str1 == str2); msg += Environment.NewLine; msg += String.Format("str1.Equals(str3) => {0}", str1.Equals(str3)); msg += Environment.NewLine; msg += str4; msg += Environment.NewLine; msg += str5; msg += Environment.NewLine; msg += str6; msg += Environment.NewLine; Console.WriteLine(msg); 9
  • 12. “” & STRING.EMPTY static void Main(string[] args) { int count = 1000000000; EmptyString1(count); EmptyString2(count); EmptyString3(count); } private static void EmptyString1(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = ""; } sw.Stop(); Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString2(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = string.Empty; } sw.Stop(); Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString3(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); } sw.Stop(); Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString()); } 12
  • 13. “” & STRING.EMPTY static void Main(string[] args) { int count = 1000000000; EmptyString1(count); EmptyString2(count); EmptyString3(count); } private static void EmptyString1(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = ""; } sw.Stop(); Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString2(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = string.Empty; } sw.Stop(); Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString3(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); } sw.Stop(); Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString()); } 130 1000 2000 3000 4000 5000 6000 7000 1000000 10000000 1000000000 "" String.Empty new String()
  • 14. “” & STRING.EMPTY static void Main(string[] args) { int count = 1000000000; EmptyString1(count); EmptyString2(count); EmptyString3(count); } private static void EmptyString1(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = ""; } sw.Stop(); Console.WriteLine("EmptyString1: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString2(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = string.Empty; } sw.Stop(); Console.WriteLine("EmptyString2: " + sw.ElapsedMilliseconds.ToString()); } private static void EmptyString3(int count) { String test; Stopwatch sw = Stopwatch.StartNew(); for (int idx = 0; idx < count; ++idx) { test = new String(' ',0); } sw.Stop(); Console.WriteLine("EmptyString3: " + sw.ElapsedMilliseconds.ToString()); } 140 1000 2000 3000 4000 5000 6000 7000 1000000 10000000 1000000000 "" String.Empty new String()
  • 15. NULL & EMPTY CHECK 15
  • 16. NULL & EMPTY CHECK  Check with == null  e.x. if (str == nill)  Check with == ""  e.x. if (str == “”)  Check with == String.Empty  e.x. if (str == String.Empty)  Check with String.IsNullOrEmpty(str)  e.x. if (String.IsNullOrEmpty(str))  Check with String.Length == 0  e.x. if (str.Length == 0) 16
  • 17. NULL & EMPTY CHECK private static void Main(string[] args) { string[] testStrings = new string[] { null, "", string.Empty }; foreach (string str in testStrings) { TestString(str); Console.WriteLine("======================="); } } private static void TestString(string str) { if (str == null) Console.WriteLine("str = null"); if (str == "") Console.WriteLine("str = """); if (str == string.Empty) Console.WriteLine("str = String.Empty"); if (string.IsNullOrEmpty(str)) Console.WriteLine("String.IsNullOrEmpty(str)"); try { if (str.Length == 0) Console.WriteLine("str.Length = 0"); } catch (Exception ex) { } } 17
  • 18. NULL & EMPTY CHECK private static void Main(string[] args) { string[] testStrings = new string[] { null, "", string.Empty }; foreach (string str in testStrings) { TestString(str); Console.WriteLine("======================="); } } private static void TestString(string str) { var count = 100000000; Console.WriteLine("str == null: {0} ms", DoTest(count, () => { var result = str == null; })); Console.WriteLine("str == "": {0} ms", DoTest(count, () => { var result = str == ""; })); Console.WriteLine("str == String.Empty: {0} ms", DoTest(count, () => { var result = str == String.Empty; })); Console.WriteLine("string.IsNullOrEmpty(str): {0} ms", DoTest(count, () => { var result = string.IsNullOrEmpty(str); })); try { Console.WriteLine("str.Length == 0: {0} ms", DoTest(count, () => { var result = str.Length == 0; })); } catch { } } static long DoTest(int count, Action action) { var sw = Stopwatch.StartNew(); for (int i = 0; i < count; ++i) action(); return sw.ElapsedMilliseconds; } 18
  • 19. NULL & EMPTY CHECK private static void Main(string[] args) { string[] testStrings = new string[] { null, "", string.Empty }; foreach (string str in testStrings) { TestString(str); Console.WriteLine("======================="); } } private static void TestString(string str) { var count = 100000000; Console.WriteLine("str == null: {0} ms", DoTest(count, () => { var result = str == null; })); Console.WriteLine("str == "": {0} ms", DoTest(count, () => { var result = str == ""; })); Console.WriteLine("str == String.Empty: {0} ms", DoTest(count, () => { var result = str == String.Empty; })); Console.WriteLine("string.IsNullOrEmpty(str): {0} ms", DoTest(count, () => { var result = string.IsNullOrEmpty(str); })); try { Console.WriteLine("str.Length == 0: {0} ms", DoTest(count, () => { var result = str.Length == 0; })); } catch { } } static long DoTest(int count, Action action) { var sw = Stopwatch.StartNew(); for (int i = 0; i < count; ++i) action(); return sw.ElapsedMilliseconds; } 19
  • 21. STRING POOL var str1 = "aaaa"; var str2 = "aa" + "aa"; var str3 = "aa"; str3 += str3; var str4 = new string('a', 4); var str5 = string.Intern(str4); Debug.WriteLine(ReferenceEquals(str1, str2)); Debug.WriteLine(ReferenceEquals(str1, str3)); Debug.WriteLine(ReferenceEquals(str1, str4)); Debug.WriteLine(ReferenceEquals(str1, str5)); 21
  • 22. STRING POOL var str1 = "aaaa"; var str2 = "aa" + "aa"; var str3 = "aa"; str3 += str3; var str4 = new string('a', 4); var str5 = string.Intern(str4); Debug.WriteLine(ReferenceEquals(str1, str2)); Debug.WriteLine(ReferenceEquals(str1, str3)); Debug.WriteLine(ReferenceEquals(str1, str4)); Debug.WriteLine(ReferenceEquals(str1, str5)); 22
  • 23. STRING POOL var str1 = "aaaa"; var str2 = "aa" + "aa"; var str3 = "aa"; str3 += str3; var str4 = new string('a', 4); var str5 = string.Intern(str4); Debug.WriteLine(ReferenceEquals(str1, str2)); Debug.WriteLine(ReferenceEquals(str1, str3)); Debug.WriteLine(ReferenceEquals(str1, str4)); Debug.WriteLine(ReferenceEquals(str1, str5)); 23 str1 str2 str3 str4 “aaaa” “aaaa” str5 “aaaa” String Pool
  • 25. STRING PROPERTY internal class Program { public String Name { get; set; } } 25
  • 26. STRING PROPERTY internal class Program { public String Name { get; set; } } 26
  • 27. STRING PROPERTY internal class Program { public String Name { get; set; } } 27
  • 28. STRING PROPERTY internal class Program { private String _name; public String Name { get { return _name ?? String.Empty; } set { _name = value; } } } 28
  • 30. STRINGBUILDER  Consider using the String class under these conditions:  When the number of changes that your app will make to a string is small. In these cases, StringBuilder might offer negligible or no performance improvement over String.  When you are performing a fixed number of concatenation operations, particularly with string literals. In this case, the compiler might combine the concatenation operations into a single operation.  When you have to perform extensive search operations while you are building your string. The StringBuilder class lacks search methods such as IndexOf or StartsWith. You'll have to convert the StringBuilderobject to a String for these operations, and this can negate the performance benefit from using StringBuilder. For more information, see the Searching the text in a StringBuilder object section. 30
  • 31. STRINGBUILDER  Consider using the StringBuilder class under these conditions:  When you expect your app to make an unknown number of changes to a string at design time (for example, when you are using a loop to concatenate a random number of strings that contain user input).  When you expect your app to make a significant number of changes to a string. 31
  • 32. STRINGBUILDER var count = 1000000; var result = string.Empty; var sw = Stopwatch.StartNew(); for (var index = 0; index < count; ++index) result += "a"; Console.WriteLine(sw.ElapsedMilliseconds); result = string.Empty; sw.Restart(); var sb = new StringBuilder(); for (var index = 0; index < count; ++index) sb.Append("a"); result = sb.ToString(); Console.WriteLine(sw.ElapsedMilliseconds); 32 0 50000 100000 150000 200000 250000 300000 350000 1000 10000 100000 1000000 String concat StringBuilder “aaaa” 164 “aaaaaaaaaaaaaaaa” 16 “aaaaaaaaaaaaaaaaa” 3217 “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa” 6433
  • 34. REFERENCE  [.Net Concept]理解並善用String pool - Level Up- 點 部落  http://www.dotblogs.com.tw/larrynung/archive/2011/06/3 0/30763.aspx  [Performance][C#]String.Empty V.S “” - Level Up- 點部落  http://www.dotblogs.com.tw/larrynung/archive/2009/12/2 2/12615.aspx  [Performance][VB.NET].NET空字串判斷徹底研究 - Level Up- 點部落  http://www.dotblogs.com.tw/larrynung/archive/2009/03/1 3/7461.aspx 34
  • 35. REFERENCE  [.NET Concept]集合與字串類型的屬性和方法應避免 回傳null - Level Up- 點部落  http://www.dotblogs.com.tw/larrynung/archive/2011/04/1 6/22866.aspx  String與Stringbuilder組字串的效能比較 - Jeff 隨手記 - 點部落  http://www.dotblogs.com.tw/jeff- yeh/archive/2008/11/04/5870.aspx  [C#.NET] 動態處理字串 - StringBuilder 類別 與 String 類別的效能 - 余小章 @ 大內殿堂- 點部落  http://www.dotblogs.com.tw/yc421206/archive/2010/10/ 26/18575.aspx 35
  • 36. REFERENCE  StringBuilder串接字串的迷思 - 黑暗執行緒  http://blog.darkthread.net/blogs/darkthreadtw/archive/20 07/12/15/stringbuilder-for-static-string-concate.aspx  Heikniemi Hardcoded » .net String vs. StringBuilder – concatenation performance  http://www.heikniemi.net/hardcoded/2004/08/net-string- vs-stringbuilder-concatenation-performance/  StringBuilder Class (System.Text)  https://msdn.microsoft.com/en- us//library/system.text.stringbuilder.aspx 36
  • 37. REFERENCE  文章-StringBuilder與String的字串相接效能大車拼 - 黑暗執行緒  http://blog.darkthread.net/blogs/darkthreadtw/archive/20 09/09/07/article-stringbuilder-vs-string.aspx  Performance considerations for strings in C# - CodeProject  http://www.codeproject.com/Articles/10318/Performance -considerations-for-strings-in-C  Improving String Handling Performance in .NET Framework Applications  https://msdn.microsoft.com/en- us/library/aa302329.aspx?f=255&MSPPError=- 2147217396 37
  • 38. REFERENCE  Introduction to Programming with C# / Java Books » Chapter 13. Strings and Text Processing  http://www.introprogramming.info/english-intro-csharp- book/read-online/chapter-13-strings-and-text- processing/ 38

Editor's Notes

  1. 在這些條件下考慮使用 String 類別: - 當應用程式對字串進行變更的數目很小。 在這些情況下, StringBuilder 不可能在 String 中提供無關緊要的效能改善。 - 當您執行串連作業固定數字,特別是字串常值時。 在這種情況下,編譯器可以結合串連作業至單一作業。 - 當建置字串時,您必須執行大量搜尋。 StringBuilder 類別表示搜尋方法 (例如 IndexOf 或 StartsWith。 您必須將 StringBuilder物件轉換成這些作業的 String ,因此,這可能使 StringBuilder 的效能優勢失去效用。 如需詳細資訊,請參閱 搜尋 StringBuilder 物件中的文字 一節。
  2. 在這些條件下考慮使用 StringBuilder 類別: - 在設計階段中,當您希望應用程式對字串做變更 (例如,當您使用迴圈串連包含使用者輸入字串的亂數)。 - 當您希望您的應用程式進行大量資料的變更。