String.Format and composite format strings
The static Format method provides a convenient way to build strings that embed
variables. The embedded variables can be of any type; the Format simply calls
ToString on them.
The master string that includes the embedded variables is called a composite format
string. When calling String.Format, you provide a composite format string followed
by each of the embedded variables. For example:
string composite = "It's {0} degrees in {1} on this {2} morning";
string s = string.Format (composite, 35, "Perth", DateTime.Now.DayOfWeek);
// s == "It's 35 degrees in Perth on this Friday morning"
(And that’s Celsius!)
Each number in curly braces is called a format item. The number corresponds to the
argument position and is optionally followed by:
• A comma and a minimum width to apply
• A colon and a format string
The minimum width is useful for aligning columns. If the value is negative, th
is left-aligned; otherwise, it’s right-aligned. For example:
string composite = "Name={0,-20} Credit Limit={1,15:C}";
Console.WriteLine (string.Format (composite, "Mary", 500));
Console.WriteLine (string.Format (composite, "Elizabeth", 20000));
Here’s the result:
Name=Mary Credit Limit= $500.00
Name=Elizabeth Credit Limit= $20,000.00
The equivalent without using string.Format is this:
string s = "Name=" + "Mary".PadRight (20) +
" Credit Limit=" + 500.ToString ("C").PadLeft (15);
Monday, 26 November 2012
c# for dummies: c sharp classes
c# for dummies: c sharp classes: Splitting and joining strings Split takes a sentence and returns an array of words: string[] words = "The quick brown fox".Split(); foreach ...
c sharp classes
Splitting and joining strings
Split takes a sentence and returns an array of words:
string[] words = "The quick brown fox".Split();
foreach (string word in words)
Console.Write (word + "|"); // The|quick|brown|fox|
By default, Split uses whitespace characters as delimiters; it’s also overloaded to
accept a params array of char or string delimiters. Split also optionally accepts a
StringSplitOptions enum, which has an option to remove empty entries: this is
useful when words are separated by several delimiters in a row.
The static Join method does the reverse of Split. It requires a delimiter and string
array:
string[] words = "The quick brown fox".Split();
string together = string.Join (" ", words); // The quick brown fox
The static Concat method is similar to Join but accepts only a params string array and
applies no separator. Concat is exactly equivalent to the + operator (the compiler, in
fact, translates + to Concat):
string sentence = string.Concat ("The", " quick", " brown", " fox");
string sameSentence = "The" + " quick" + " brown" + " fox";
Split takes a sentence and returns an array of words:
string[] words = "The quick brown fox".Split();
foreach (string word in words)
Console.Write (word + "|"); // The|quick|brown|fox|
By default, Split uses whitespace characters as delimiters; it’s also overloaded to
accept a params array of char or string delimiters. Split also optionally accepts a
StringSplitOptions enum, which has an option to remove empty entries: this is
useful when words are separated by several delimiters in a row.
The static Join method does the reverse of Split. It requires a delimiter and string
array:
string[] words = "The quick brown fox".Split();
string together = string.Join (" ", words); // The quick brown fox
The static Concat method is similar to Join but accepts only a params string array and
applies no separator. Concat is exactly equivalent to the + operator (the compiler, in
fact, translates + to Concat):
string sentence = string.Concat ("The", " quick", " brown", " fox");
string sameSentence = "The" + " quick" + " brown" + " fox";
c# for dummies: c sharp classes
c# for dummies: c sharp classes: Manipulating strings Because String is immutable, all the methods that “manipulate” a string ret...
c sharp classes
Manipulating strings
Because String is immutable, all the methods that “manipulate” a string return a
new one, leaving the original untouched (the same goes for when you reassign a
string variable).
Substring extracts a portion of a string:
string left3 = "12345".Substring (0, 3); // left3 = "123";
string mid3 = "12345".Substring (1, 3); // mid3 = "234";
If you omit the length, you get the remainder of the string:
string end3 = "12345".Substring (2); // end3 = "345";
Insert and Remove insert or remove characters at a specified position:
string s1 = "helloworld".Insert (5, ", "); // s1 = "hello, world"
string s2 = s1.Remove (5, 2); // s2 = "helloworld";
PadLeft and PadRight pad a string to a given length with a specified character (or a
space if unspecified):
Console.WriteLine ("12345".PadLeft (9, '*')); // ****12345
Console.WriteLine ("12345".PadLeft (9)); // 12345
If the input string is longer than the padding length, the original string is returned
unchanged.
TrimStart and TrimEnd remove specified characters from the beginning or end of a
string; Trim does both. By default, these functions remove whitespace characters
(including spaces, tabs, new lines, and Unicode variations of these):
Console.WriteLine (" abc \t\r\n ".Trim().Length); // 3
Replace replaces all occurrences of a particular character or substring:
Console.WriteLine ("to be done".Replace (" ", " | ") ); // to | be | done
Console.WriteLine ("to be done".Replace (" ", "") ); // tobedone
ToUpper and ToLower return upper- and lowercase versions of the input string. By
default, they honor the user’s current language settings; ToUpperInvariant and
ToLowerInvariant always apply English alphabet rules.
Because String is immutable, all the methods that “manipulate” a string return a
new one, leaving the original untouched (the same goes for when you reassign a
string variable).
Substring extracts a portion of a string:
string left3 = "12345".Substring (0, 3); // left3 = "123";
string mid3 = "12345".Substring (1, 3); // mid3 = "234";
If you omit the length, you get the remainder of the string:
string end3 = "12345".Substring (2); // end3 = "345";
Insert and Remove insert or remove characters at a specified position:
string s1 = "helloworld".Insert (5, ", "); // s1 = "hello, world"
string s2 = s1.Remove (5, 2); // s2 = "helloworld";
PadLeft and PadRight pad a string to a given length with a specified character (or a
space if unspecified):
Console.WriteLine ("12345".PadLeft (9, '*')); // ****12345
Console.WriteLine ("12345".PadLeft (9)); // 12345
If the input string is longer than the padding length, the original string is returned
unchanged.
TrimStart and TrimEnd remove specified characters from the beginning or end of a
string; Trim does both. By default, these functions remove whitespace characters
(including spaces, tabs, new lines, and Unicode variations of these):
Console.WriteLine (" abc \t\r\n ".Trim().Length); // 3
Replace replaces all occurrences of a particular character or substring:
Console.WriteLine ("to be done".Replace (" ", " | ") ); // to | be | done
Console.WriteLine ("to be done".Replace (" ", "") ); // tobedone
ToUpper and ToLower return upper- and lowercase versions of the input string. By
default, they honor the user’s current language settings; ToUpperInvariant and
ToLowerInvariant always apply English alphabet rules.
c# for dummies: c# for dummies
c# for dummies: c# for dummies: using System; class StringMethods { public static void Main( string[] args ) { string string1 = "hello there"; char[] charact...
c# for dummies
using System;
class StringMethods
{
public static void Main( string[] args )
{
string string1 = "hello there";
char[] characterArray = new char[ 5 ];
// output string1
Console.WriteLine( "string1: \"" + string1 + "\"" );
// loop through characters in string1 and display reversed
Console.Write( "The string reversed is: " );
for ( int i = - 1; i >= 0; i-- )
Console.Write( );
// copy characters from string1 into characterArray
Console.Write( "\nThe character array is: " );
for ( int i = 0; i < ; i++ )
Console.Write( );
Console.WriteLine( "\n" );
} // end Main
} // end class StringMethods
// test Length property
Console.WriteLine( "Length of string1: " + string1.Length );
string1.Length
string1[ i ]
string1.CopyTo( 0, characterArray, 0, characterArray.Length );
characterArray.Length
characterArray[ i ]
ouput
string1: "hello there"
Length of string1: 11
The string reversed is: ereht olleh
The character array is: hello
class StringMethods
{
public static void Main( string[] args )
{
string string1 = "hello there";
char[] characterArray = new char[ 5 ];
// output string1
Console.WriteLine( "string1: \"" + string1 + "\"" );
// loop through characters in string1 and display reversed
Console.Write( "The string reversed is: " );
for ( int i = - 1; i >= 0; i-- )
Console.Write( );
// copy characters from string1 into characterArray
Console.Write( "\nThe character array is: " );
for ( int i = 0; i < ; i++ )
Console.Write( );
Console.WriteLine( "\n" );
} // end Main
} // end class StringMethods
// test Length property
Console.WriteLine( "Length of string1: " + string1.Length );
string1.Length
string1[ i ]
string1.CopyTo( 0, characterArray, 0, characterArray.Length );
characterArray.Length
characterArray[ i ]
ouput
string1: "hello there"
Length of string1: 11
The string reversed is: ereht olleh
The character array is: hello
Subscribe to:
Posts (Atom)