Monday, 26 November 2012
c# for dummies: c# for dummies
c# for dummies: c# for dummies: Strings Manipulation public static void Main( string[] args ) { Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n...
c# for dummies
Strings Manipulation
public static void Main( string[] args )
{
Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n" +
"string2 = " + "\"" + string2 + "\"\n" +
"string3 = " + "\"" + string3 + "\"\n" +
"string4 = " + "\"" + string4 + "\"\n" );
} // end Main
// end class StringConstructor
// string initialization
char[] characterArray =
{ 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
string originalString = "Welcome to C# programming!";
string string1 = originalString;
string string2 = new string( characterArray );
string string3 = new string( characterArray, 6, 3 );
string string4 = new string( 'C', 5 );
}
output
string1 = "Welcome to C# programming!"
string2 = "birth day"
string3 = "day"
string4 = "CCCCC"
public static void Main( string[] args )
{
Console.WriteLine( "string1 = " + "\"" + string1 + "\"\n" +
"string2 = " + "\"" + string2 + "\"\n" +
"string3 = " + "\"" + string3 + "\"\n" +
"string4 = " + "\"" + string4 + "\"\n" );
} // end Main
// end class StringConstructor
// string initialization
char[] characterArray =
{ 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
string originalString = "Welcome to C# programming!";
string string1 = originalString;
string string2 = new string( characterArray );
string string3 = new string( characterArray, 6, 3 );
string string4 = new string( 'C', 5 );
}
output
string1 = "Welcome to C# programming!"
string2 = "birth day"
string3 = "day"
string4 = "CCCCC"
c# for dummies: c sharp programming
c# for dummies: c sharp programming: The System.Text.StringBuilder Type Given that the string type can be inefficient when used with reckless abandon, the .NET base class librar...
c# for dummies: c sharp programming
c# for dummies: c sharp programming: The System.Text.StringBuilder Type Given that the string type can be inefficient when used with reckless abandon, the .NET base class librar...
c sharp programming
The System.Text.StringBuilder Type
Given that the string type can be inefficient when used with reckless abandon, the .NET base class
libraries provide the System.Text namespace. Within this (relatively small) namespace lives a class
named StringBuilder. Like the System.String class, the StringBuilder defines methods that allow you
to replace or format segments, for example. When you wish to use this type in your C# code files, your
first step is to make sure the following namespace is imported into your code file:
// StringBuilder lives here!
using System.Text;
What is unique about the StringBuilder is that when you call members of this type, you are directly
modifying the object’s internal character data (making it more efficient), not obtaining a copy of the data
in a modified format. When you create an instance of the StringBuilder, you can supply the object’s
initial startup values via one of many constructors. If you are new to the topic of constructors, simply
understand that constructors allow you to create an object with an initial state when you apply the new
keyword. Consider the following usage of StringBuilder:
static void FunWithStringBuilder()
{
Console.WriteLine("=> Using the StringBuilder:");
StringBuilder sb = new StringBuilder("**** Fantastic Games ****");
sb.Append("\n");
sb.AppendLine("Half Life");
sb.AppendLine("Beyond Good and Evil");
sb.AppendLine("Deus Ex 2");
sb.AppendLine("System Shock");
Console.WriteLine(sb.ToString());
sb.Replace("2", "Invisible War");
Console.WriteLine(sb.ToString());
Console.WriteLine("sb has {0} chars.", sb.Length);
Console.WriteLine();
}
Here we have constructed a StringBuilder set to the initial value "**** Fantastic Games ****". As
you can see, we are appending to the internal buffer and are able to replace or remove characters at will.
By default, a StringBuilder is only able to initially hold a string of 16 characters or fewer (but will expand
automatically if necessary); however, this default starting value can be changed via an additional
constructor argument.
Given that the string type can be inefficient when used with reckless abandon, the .NET base class
libraries provide the System.Text namespace. Within this (relatively small) namespace lives a class
named StringBuilder. Like the System.String class, the StringBuilder defines methods that allow you
to replace or format segments, for example. When you wish to use this type in your C# code files, your
first step is to make sure the following namespace is imported into your code file:
// StringBuilder lives here!
using System.Text;
What is unique about the StringBuilder is that when you call members of this type, you are directly
modifying the object’s internal character data (making it more efficient), not obtaining a copy of the data
in a modified format. When you create an instance of the StringBuilder, you can supply the object’s
initial startup values via one of many constructors. If you are new to the topic of constructors, simply
understand that constructors allow you to create an object with an initial state when you apply the new
keyword. Consider the following usage of StringBuilder:
static void FunWithStringBuilder()
{
Console.WriteLine("=> Using the StringBuilder:");
StringBuilder sb = new StringBuilder("**** Fantastic Games ****");
sb.Append("\n");
sb.AppendLine("Half Life");
sb.AppendLine("Beyond Good and Evil");
sb.AppendLine("Deus Ex 2");
sb.AppendLine("System Shock");
Console.WriteLine(sb.ToString());
sb.Replace("2", "Invisible War");
Console.WriteLine(sb.ToString());
Console.WriteLine("sb has {0} chars.", sb.Length);
Console.WriteLine();
}
Here we have constructed a StringBuilder set to the initial value "**** Fantastic Games ****". As
you can see, we are appending to the internal buffer and are able to replace or remove characters at will.
By default, a StringBuilder is only able to initially hold a string of 16 characters or fewer (but will expand
automatically if necessary); however, this default starting value can be changed via an additional
constructor argument.
c# for dummies: c sharp programming
c# for dummies: c sharp programming: Strings Are Immutable One of the interesting aspects of System.String is that once you assign a string object with its initial value, the ...
c sharp programming
Strings Are Immutable
One of the interesting aspects of System.String is that once you assign a string object with its initial
value, the character data cannot be changed. At first glance, this might seem like a flat-out lie, given that
we are always reassigning strings to new values and because the System.String type defines a number of
methods that appear to modify the character data in one way or another (such as uppercasing and
lowercasing). However, if you look more closely at what is happening behind the scenes, you will notice
the methods of the string type are, in fact, returning you a brand-new string object in a modified
format.
static void StringsAreImmutable()
{
// Set initial string value.
string s1 = "This is my string.";
Console.WriteLine("s1 = {0}", s1);
// Uppercase s1?
string upperString = s1.ToUpper();
Console.WriteLine("upperString = {0}", upperString);
// Nope! s1 is in the same format!
Console.WriteLine("s1 = {0}", s1);
}
One of the interesting aspects of System.String is that once you assign a string object with its initial
value, the character data cannot be changed. At first glance, this might seem like a flat-out lie, given that
we are always reassigning strings to new values and because the System.String type defines a number of
methods that appear to modify the character data in one way or another (such as uppercasing and
lowercasing). However, if you look more closely at what is happening behind the scenes, you will notice
the methods of the string type are, in fact, returning you a brand-new string object in a modified
format.
static void StringsAreImmutable()
{
// Set initial string value.
string s1 = "This is my string.";
Console.WriteLine("s1 = {0}", s1);
// Uppercase s1?
string upperString = s1.ToUpper();
Console.WriteLine("upperString = {0}", upperString);
// Nope! s1 is in the same format!
Console.WriteLine("s1 = {0}", s1);
}
Subscribe to:
Posts (Atom)