- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Strings in C#
In this part of the C# tutorial, we will work with string data in more detail. Strings are very important in computer languages. That is why we dedicate a whole chapter to working with strings in C#.
A string is a sequence of characters. In C#, a string is a sequence of Unicode characters. It is a data type which stores a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding. When a string appears literally in the source code, it is known as a string literal.
Strings are objects. There are two basic classes for working with strings:
- System.String
- System.Text.StringBuilder
The String
is an immutable sequence of characters. The StringBuilder
is a mutable sequence of characters.
In C#, string
is an alias for System.String
. The string
is a language keyword and the System.String
is a .NET type.
Initializing strings
There are multiple ways of creating strings, both immutable and mutable. We will show a few of them.
using System; using System.Text; public class Initialization { static void Main() { char[] cdb = {'M', 'y', 'S', 'q', 'l'}; string lang = "C#"; String ide = "NetBeans"; string db = new string(cdb); Console.WriteLine(lang); Console.WriteLine(ide); Console.WriteLine(db); StringBuilder sb1 = new StringBuilder(lang); StringBuilder sb2 = new StringBuilder(); sb2.Append("Fields"); sb2.Append(" of "); sb2.Append("glory"); Console.WriteLine(sb1); Console.WriteLine(sb2); } }
The example shows a few ways of creating System.String
and System.Text.StringBuilder
objects.
using System.Text;
This statement enables to use the System.Text.StringBuilder
type without qualification.
string lang = "C#"; String ide = "NetBeans";
The most common way is to create a string object from a string literal.
string db = new string(cdb);
Here we create a string object from an array of characters. The string
is an alias for the System.String
.
StringBuilder sb1 = new StringBuilder(lang);
A StringBuilder
object is created from a String
.
StringBuilder sb2 = new StringBuilder(); sb2.Append("Fields"); sb2.Append(" of "); sb2.Append("glory");
We create an empty StringBuilder
object. We append three strings into the object.
$ ./initialization.exe C# NetBeans MySql C# Fields of glory
Running the example gives this result.
Strings are objects
Strings are objects. They are reference types. Strings are instances of the System.String
or System.Text.StringBuilder
class. Since they are objects, they have multiple methods available for doing various work.
using System; public class CSharpApp { static void Main() { string lang = "Java"; string bclass = lang.GetType().Name; Console.WriteLine(bclass); string parclass = lang.GetType().BaseType.Name; Console.WriteLine(parclass); if (lang.Equals(String.Empty)) { Console.WriteLine("The string is empty"); } else { Console.WriteLine("The string is not empty"); } int l = lang.Length; Console.WriteLine("The string has " + l + " characters"); } }
In this program, we demonstrate that strings are objects. Objects must have a class name, a parent class and they must also have some methods that we can call or properties to access.
string lang = "Java";
An object of System.String
type is created.
string bclass = lang.GetType().Name; Console.WriteLine(bclass);
We determine the class name of the object to which the lang
variable refers.
string parclass = lang.GetType().BaseType.Name; Console.WriteLine(parclass);
A parent class of our object is received. All objects have at least one parent — the Object
.
if (lang.Equals(String.Empty)) { Console.WriteLine("The string is empty"); } else { Console.WriteLine("The string is not empty"); }
Objects have various methods. With the Equals()
method we check if the string is empty.
int l = lang.Length;
The Length
method returns the size of the string.
$ ./stringobjects.exe String Object The string is not empty The string has 4 characters
This is the output of the stringobjects.exe program.
Mutable & immutable strings
The String
is a sequence of immutable characters, while the StringBuilder
is a sequence of mutable characters. The next example will show the difference.
using System; using System.Text; public class CSharpApp { static void Main() { string name = "Jane"; string name2 = name.Replace('J', 'K'); string name3 = name2.Replace('n', 't'); Console.WriteLine(name); Console.WriteLine(name3); StringBuilder sb = new StringBuilder("Jane"); Console.WriteLine(sb); sb.Replace('J', 'K', 0, 1); sb.Replace('n', 't', 2, 1); Console.WriteLine(sb); } }
Both objects have methods for replacing characters in a string.
string name = "Jane"; string name2 = name.Replace('J', 'K'); string name3 = name2.Replace('n', 't');
Calling a Replace()
method on a String
results in returning a new modified string. The original string is not changed.
sb.Replace('J', 'K', 0, 1); sb.Replace('n', 't', 2, 1);
The Replace()
method of a StringBuilder
will replace a character at the given index with a new character. The original string is modified.
$ ./mutableimmutable.exe Jane Kate Jane Kate
This is the output of the mutableimmutable.exe
program.
Concatenating strings
Immutable strings can be added using the + operator or the Concat()
method. They will form a new string which is a chain of all concatenated strings. Mutable strings have the Append()
method which builds a string from any number of other strings.
using System; using System.Text; public class Concatenate { static void Main() { Console.WriteLine("Return" + " of " + "the king."); Console.WriteLine(string.Concat(string.Concat("Return", " of "), "the king.")); StringBuilder sb = new StringBuilder(); sb.Append("Return"); sb.Append(" of "); sb.Append("the king."); Console.WriteLine(sb); } }
The example creates three sentences by adding strings.
Console.WriteLine("Return" + " of " + "the king.");
A new string is formed by using the + operator.
Console.WriteLine(string.Concat(string.Concat("Return", " of "), "the king."));
The Concat()
method concatenates two strings. The method is a static method of the System.String
class.
StringBuilder sb = new StringBuilder(); sb.Append("Return"); sb.Append(" of "); sb.Append("the king.");
A mutable object of the StringBuilder
type is created by calling the Append()
method three times.
$ ./concatenate.exe Return of the king. Return of the king. Return of the king.
This is the example output.
Using quotes
What if we wanted to display quotes, for example in a direct speech? In such a case, inner quotes must be escaped.
using System; public class Quotes { static void Main() { Console.WriteLine("There are many stars."); Console.WriteLine("He said, \"Which one is your favourite?\""); } }
We use the \
character to escape additional quotes.
$ ./quotes.exe There are many stars. He said, "Which one is your favourite?"
This is the output of the quotes.exe program.
Multiline strings
It is possible to create a multiline string in C#.
using System; public class Multiline { static void Main() { string multiString = @"I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good"; Console.WriteLine(multiString); } }
A multiline string can be created easily by using the @
character at the beginning of the string.
$ ./multiline.exe I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good
Comparing strings
We can compare two strings with the ==
operator.
using System; public class CSharpApp { static void Main() { Console.WriteLine("12" == "12"); Console.WriteLine("17" == "9"); Console.WriteLine("aa" == "ab"); } }
In the example program, we compare strings.
$ ./compare.exe True False False
This is the output of the program.
There is a string.Compare()
method, which compares two specified strings and returns an integer that indicates their relative position in the sort order. If the returned value is less than zero, the first string is less than the second. If it returns zero, both strings are equal. Finally, if the returned value is greater than zero, the first string is greater than the second.
using System; public class CSharpApp { static void Main() { string str1 = "ZetCode"; string str2 = "zetcode"; Console.WriteLine(string.Compare(str1, str2, true)); Console.WriteLine(string.Compare(str1, str2, false)); } }
There is an optional third ignoreCase argument. It determines if the case should be honored or not.
Console.WriteLine(string.Compare(str1, str2, true));
Compare two strings and ignore the case. This line prints 0 to the console.
String elements
A string is a sequence of characters. A character is a basic element of a string.
using System; public class Elements { static void Main() { char[] crs = {'Z', 'e', 't', 'C', 'o', 'd', 'e' }; String s = new String(crs); char c1 = s[0]; char c2 = s[(s.Length-1)]; Console.WriteLine(c1); Console.WriteLine(c2); int i1 = s.IndexOf('e'); int i2 = s.LastIndexOf('e'); Console.WriteLine("The first index of character e is " + i1); Console.WriteLine("The last index of character e is " + i2); Console.WriteLine(s.Contains("t")); Console.WriteLine(s.Contains("f")); char[] elements = s.ToCharArray(); foreach (char el in elements) { Console.WriteLine(el); } } }
In the first example, we will work with an immutable string.
char[] crs = {'Z', 'e', 't', 'C', 'o', 'd', 'e' }; String s = new String(crs);
A new immutable string is formed from an array of characters.
char c1 = s[0]; char c2 = s[(s.Length-1)];
Using the array access notation, we we get the first and the last char value of the string.
int i1 = s.IndexOf('e'); int i2 = s.LastIndexOf('e');
With the above methods, we get the first and the last occurrence of the character 'e'.
Console.WriteLine(s.Contains("t")); Console.WriteLine(s.Contains("f"));
With the Contains()
method we check if the string contains the 't' character. The method returns a boolean value.
char[] elements = s.ToCharArray(); foreach (char el in elements) { Console.WriteLine(el); }
The ToCharArray()
method creates a character array from the string. We go through the array and print each of the characters.
$ ./stringelemetns.exe Z e The first index of character e is 1 The last index of character e is 6 True False Z e t C o d e
This is the example output.
In the second example, we will work with the elements of a mutable string.
using System; using System.Text; public class StringBuilderElements { static void Main() { StringBuilder sb = new StringBuilder("Misty mountains"); Console.WriteLine(sb); sb.Remove(sb.Length-1, 1); Console.WriteLine(sb); sb.Append('s'); Console.WriteLine(sb); sb.Insert(0, 'T'); sb.Insert(1, 'h'); sb.Insert(2, 'e'); sb.Insert(3, ' '); Console.WriteLine(sb); sb.Replace('M', 'm', 4, 1); Console.WriteLine(sb); } }
A mutable string is formed. We modify the contents of the string by deleting, appending, inserting, and replacing characters.
sb.Remove(sb.Length-1, 1);
This line deletes the last character.
sb.Append('s');
The deleted character is appended back to the string.
sb.Insert(0, 'T'); sb.Insert(1, 'h'); sb.Insert(2, 'e'); sb.Insert(3, ' ');
We insert four characters at the beginning of the string.
sb.Replace('M', 'm', 4, 1);
Finally, we replace a character at index 4.
$ ./stringbuilderelements.exe Misty mountains Misty mountain Misty mountains The Misty mountains The misty mountains
From the output we can see how the mutable string is changing.
String methods
C# has useful built-in methods that can be used for working with strings.
The Join()
and Split()
are very handy methods.
using System; public class JoinSplit { static void Main() { string[] items = new string[] {"C#", "Visual Basic", "Java", "Perl"}; string langs = string.Join(",", items); Console.WriteLine(langs); string[] ls = langs.Split(','); foreach (string lang in ls) { Console.WriteLine(lang); } } }
In our program, we will join and split strings.
string[] items = new string[] {"C#", "Visual Basic", "Java", "Perl"};
This is an array of strings. These strings are going to be joined.
string langs = string.Join(",", items);
All words from the array are joined. We build one string from them. There will be a comma character between each two words.
string[] ls = langs.Split(',');
As a reverse operation, we split the langs string. The Split()
method returns an array of words, delimited by a character. In our case it is a comma character.
foreach (string lang in ls) { Console.WriteLine(lang); }
We go through the array and print its elements.
$ ./joinsplit.exe C#,Visual Basic,Java,Perl C# Visual Basic Java Perl
This is the output of the example.
using System; public class StringMethods { static void Main() { string str = "Determination"; Console.WriteLine(str.Contains("e")); Console.WriteLine(str.IndexOf("e")); Console.WriteLine(str.LastIndexOf("i")); Console.WriteLine(str.ToUpper()); Console.WriteLine(str.ToLower()); } }
We introduce five string methods in the above example.
Console.WriteLine(str.Contains("e"));
The Contains()
method returns True if the string contains a specific character.
Console.WriteLine(str.IndexOf("e"));
The IndexOf()
returns the first index of a letter in the string.
Console.WriteLine(str.LastIndexOf("i"));
The LastIndexOf()
methods returns the last index of a letter in a string.
Console.WriteLine(str.ToUpper()); Console.WriteLine(str.ToLower());
Letters of the string are converted to uppercase with the ToUpper()
method and to lowercase with the ToLower()
method.
$ ./strmethods.exe True 1 10 DETERMINATION determination
Running the program.
Copy vs Clone
We will describe a difference between two methods: Copy()
and Clone()
. The Copy()
method creates a new instance of string with the same value as a specified string. The Clone()
method returns a reference to the string which is being cloned. It is not an independent copy of the string on the Heap. It is another reference on the same string.
using System; public class CopyClone { static void Main() { string str = "ZetCode"; string cloned = (string) str.Clone(); string copied = string.Copy(str); Console.WriteLine(str.Equals(cloned)); // prints true Console.WriteLine(str.Equals(copied)); // prints true Console.WriteLine(ReferenceEquals(str, cloned)); // prints true Console.WriteLine(ReferenceEquals(str, copied)); // prints false } }
Our example demonstrates the difference between the two methods.
string cloned = (string) str.Clone(); string copied = string.Copy(str);
The string value is cloned and copied.
Console.WriteLine(str.Equals(cloned)); // prints true Console.WriteLine(str.Equals(copied)); // prints true
The Equals()
method determines whether two string objects have the same value. The contents of all three strings are the same.
Console.WriteLine(ReferenceEquals(str, cloned)); // prints true Console.WriteLine(ReferenceEquals(str, copied)); // prints false
The ReferenceEquals()
method compares the two reference objects. Therefore comparing a copied string to the original string returns false. Because they are two distinct objects.
Formatting strings
In the next examples, we will format strings. The .NET Framework has a feature called composite formatting. It is supported by Format()
and WriteLine()
methods. A method takes a list of objects and a composite format string as input. The format string consists of fixed string and some format items. These format items are indexed placeholders which correspond to the objects in the list.
The format item has the following syntax:
{index[,length][:formatString]}
The index component is mandatory. It is a number starting from 0 that refers to an item from the list of objects. Multiple items can refer to the same element of the list of objects. An object is ignored if it is not referenced by a format item. If we refer outside the bounds of the list of objects, a runtime exception is thrown.
The length component is optional. It is the minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned. If it is specified, there must by a colon separating the index and the length.
The formatString is optional. It is a string that formats a value is a specific way. It can be used to format dates, times, numbers or enumerations.
Here we show, how to work with length component of the format items. We print three columns of numbers to the terminal. Left, middle and right aligned.
using System; public class Format1 { static void Main() { int oranges = 2; int apples = 4; int bananas = 3; string str1 = "There are {0} oranges, {1} apples and " + "{2} bananas"; string str2 = "There are {1} oranges, {2} bananas and " + "{0} apples"; Console.WriteLine(str1, oranges, apples, bananas); Console.WriteLine(str2, apples, oranges, bananas); } }
We print a simple message to the console. We use only index component of the format item.
string str1 = "There are {0} oranges, {1} apples and " + "{2} bananas";
The {0}
, {1}
, and {2}
are format items. We specify the index component. Other components are optional.
Console.WriteLine(str1, oranges, apples, bananas);
Now we put together the composite formatting. We have the string and the list of objects (oranges, apples, bananas). The {0}
format item refers to the oranges. The WriteLine()
method replaces the {0}
format item with the contents of the oranges variable.
string str2 = "There are {1} oranges, {2} bananas and " + "{0} apples";
The order of the format items referring to the objects is notation important.
$ ./format1.exe There are 2 oranges, 4 apples and 3 bananas There are 2 oranges, 3 bananas and 4 apples
We can see the outcome of the format1.exe
program.
The next example will format numeric data.
using System; public class Format2 { static void Main() { Console.WriteLine("{0} {1, 12}", "Decimal", "Hexadecimal"); Console.WriteLine("{0:D} {1,8:X}", 502, 546); Console.WriteLine("{0:D} {1,8:X}", 345, 765); Console.WriteLine("{0:D} {1,8:X}", 320, 654); Console.WriteLine("{0:D} {1,8:X}", 120, 834); Console.WriteLine("{0:D} {1,8:X}", 620, 454); } }
We print numbers in a decimal and hexadecimal format. We also align the numbers using the length component.
Console.WriteLine("{0:D} {1,8:X}", 502, 546);
The {0:D}
format item specifies, the first item from the list of supplied objects will be taken and formatted in the decimal format. The {1,8:X}
format item takes the second item. Formats it in the hexadecimal format :X
. And the string length will be 8 characters 8
. Because the number has only three characters, it is right aligned and padded with empty strings.
$ ./format2.exe Decimal Hexadecimal 502 222 345 2FD 320 28E 120 342 620 1C6
Running the example we get this outcome.
The last two examples will format numeric and date data.
using System; public class Format3 { static void Main() { Console.WriteLine(string.Format("Number: {0:N}", 126)); Console.WriteLine(string.Format("Scientific: {0:E}", 126)); Console.WriteLine(string.Format("Currency: {0:C}", 126)); Console.WriteLine(string.Format("Percent: {0:P}", 126)); Console.WriteLine(string.Format("Hexadecimal: {0:X}", 126)); } }
The example demonstrates the standard formatting specifiers for numbers. Number 126 is printed in five different formats: normal, scientific, currency, percent and hexadecimal.
$ ./format3.exe Number: 126.00 Scientific: 1.260000E+002 Currency: $126.00 Percent: 12,600.00 % Hexadecimal: 7E
This is the output of the format3.exe
program.
Finally, we will format date and time data.
using System; public class Format4 { static void Main() { DateTime today = DateTime.Now; Console.WriteLine(string.Format("Short date: {0:d}", today)); Console.WriteLine(string.Format("Long date: {0:D}", today)); Console.WriteLine(string.Format("Short time: {0:t}", today)); Console.WriteLine(string.Format("Long time: {0:T}", today)); Console.WriteLine(string.Format("Month: {0:M}", today)); Console.WriteLine(string.Format("Year: {0:Y}", today)); } }
The preceding example demonstrates the standard formatting specifiers for dates.
$ ./format4.exe Short date: 4/11/2011 Long date: Monday, April 11, 2011 Short time: 11:14 AM Long time: 11:14:54 AM Month: April 11 Year: April, 2011
This is the output of the example.
This part of the C# tutorial covered strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论