The System.Text Namespace
2005-09-04 20:42
309 查看
The System.Text Namespace
Just about every business application you create will eventually require you
to work with strings in some manner. String manipulation is a very common
activity in most applications, but creating, adding to, and deleting from
strings are expensive in terms of processor time and memory.
If you create a variable of type String, you are actually creating a String
object; all base data types in .NET inherit from the base Object class, so
all data types are actually objects. The contents of a String object cannot
be changed once you've placed text into the string梚n order to change its
contents, you must place the return value of a method call into a new
String object. For example, the String class provides a Remove method,
which allows you to remove a portion of a string. However, because the
contents of a String object are immutable, the Remove method doesn't do its
work "in place." Instead, it returns a new string object as its return
value, as shown here:
Dim strNew As String
Dim strOld As String = "This is a test"
' Remove the first four characters of the string.
strNew = strOld.Remove(0, 4)
If you perform many string operations that return strings, you're consuming
memory and processor cycles that you might be able to avoid using a handy
alternative: the StringBuilder class. This class manages a memory buffer
containing your text for you, and it provides methods to modify text "in
place," without requiring you to assign the result to a new string each
time you make a modification to a string. If you are performing a few
simple assignments or concatenations, don't worry about the overhead.
However, if you are building a string inside a loop, the StringBuilder
provides better performance with less overhead. The System.Text namespace
also contains classes for encoding characters into bytes and decoding bytes
into characters. Also, encoders and decoders are available for converting
to and from ASCII and Unicode.
A System.Text Example
This example creates a System.Text.StringBuilder object and provides a
string in the object's constructor. The code retrieves the seventh
character in the string and displays it in a Label control on the page. The
code invokes the Replace method to replace a substring within the string
with another string. Finally, the example inserts a new value into a
specified location within the string. (In this case, the code adds this new
value to the end of the original string.) The example in Listing 4.2 uses
the ToString method of the StringBuilder object to retrieve its contents:
Listing 4.2 Use the StringBuilder for Efficient String Handling
Private Sub StringBuilderExample()
Dim sb As New _
System.Text.StringBuilder(".NET is cool")
' Display the 7th character
lblMsg.Text = sb.Chars(6)
' Replace .NET with Microsoft.NET
sb.Replace(".NET", "Microsoft .NET")
lblMsg.Text = sb.ToString()
sb.Insert(sb.Length, " and fun.")
lblMsg.Text = sb.ToString()
End Sub
What makes this more efficient than using a normal String object is that
when you create a new StringBuilder object, it automatically manages memory
for you by creating a buffer with extra room for growth. If the buffer
needs to grow, the StringBuilder object takes care of the memory management
for you. If you want, you can pass the initial size of the buffer to the
constructor to specify how much space to allocate when a new StringBuilder
object is created.
Just about every business application you create will eventually require you
to work with strings in some manner. String manipulation is a very common
activity in most applications, but creating, adding to, and deleting from
strings are expensive in terms of processor time and memory.
If you create a variable of type String, you are actually creating a String
object; all base data types in .NET inherit from the base Object class, so
all data types are actually objects. The contents of a String object cannot
be changed once you've placed text into the string梚n order to change its
contents, you must place the return value of a method call into a new
String object. For example, the String class provides a Remove method,
which allows you to remove a portion of a string. However, because the
contents of a String object are immutable, the Remove method doesn't do its
work "in place." Instead, it returns a new string object as its return
value, as shown here:
Dim strNew As String
Dim strOld As String = "This is a test"
' Remove the first four characters of the string.
strNew = strOld.Remove(0, 4)
If you perform many string operations that return strings, you're consuming
memory and processor cycles that you might be able to avoid using a handy
alternative: the StringBuilder class. This class manages a memory buffer
containing your text for you, and it provides methods to modify text "in
place," without requiring you to assign the result to a new string each
time you make a modification to a string. If you are performing a few
simple assignments or concatenations, don't worry about the overhead.
However, if you are building a string inside a loop, the StringBuilder
provides better performance with less overhead. The System.Text namespace
also contains classes for encoding characters into bytes and decoding bytes
into characters. Also, encoders and decoders are available for converting
to and from ASCII and Unicode.
A System.Text Example
This example creates a System.Text.StringBuilder object and provides a
string in the object's constructor. The code retrieves the seventh
character in the string and displays it in a Label control on the page. The
code invokes the Replace method to replace a substring within the string
with another string. Finally, the example inserts a new value into a
specified location within the string. (In this case, the code adds this new
value to the end of the original string.) The example in Listing 4.2 uses
the ToString method of the StringBuilder object to retrieve its contents:
Listing 4.2 Use the StringBuilder for Efficient String Handling
Private Sub StringBuilderExample()
Dim sb As New _
System.Text.StringBuilder(".NET is cool")
' Display the 7th character
lblMsg.Text = sb.Chars(6)
' Replace .NET with Microsoft.NET
sb.Replace(".NET", "Microsoft .NET")
lblMsg.Text = sb.ToString()
sb.Insert(sb.Length, " and fun.")
lblMsg.Text = sb.ToString()
End Sub
What makes this more efficient than using a normal String object is that
when you create a new StringBuilder object, it automatically manages memory
for you by creating a buffer with extra room for growth. If the buffer
needs to grow, the StringBuilder object takes care of the memory management
for you. If you want, you can pass the initial size of the buffer to the
constructor to specify how much space to allocate when a new StringBuilder
object is created.
相关文章推荐
- Sending Emails in .NET with the System.Net.Mail Namespace
- The System Namespace
- The System.Data Namespace
- MMSEG: A Word Identification System for Mandarin Chinese Text Based on Two Variants of the Maximum M
- Exploring the System.Reflection.Emit Namespace(翻译)
- The System.IO Namespace
- 'ScriptServiceAttribute' is ambiguous in the namespace 'System.Web.Script.Services'
- The type or namespace name 'Windows' does not exist in the class or namespace 'System'
- The System.Collections Namespace
- The Basics of Configiration System -- ConfigNamespace and ConfigOption
- Unity3d——报错 The type or namespace name `Ports' does not exist in the namespace `System.IO'
- The System.Web.Services Namespace
- 解决System.Runtime.CompilerServices.ExtensionAttribute..ctor 与 ‘ExtensionAttribute’ is ambiguous in the namespace ‘System.Runtime.CompilerServices’ 问题
- .Net与WMI—1(Using the System.Management Namespace)
- The System.Xml Namespace
- CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data'
- 计算机专业英语-02:Text 1 The Computer System
- 解决 Xamarin 拖拽Plain Text 于Layout上时 出现 “The layout could not be loaded:java.lang.System.arraycopy([CI[CII)V” 错误
- 基于 Django1.10 文档的深入学习(20)—— Using the Django authentication system 之 login()
- 使用SecureCRT telnet登录报错:The remote system refused the connection