String formatting in C# Language
Objective
When working with language programming, formatting is the problem that always makes programmer in trouble. This tutorial will take you through some type of formatting in C#.
Let's go
Understanding String in C#
In C# language, String represents a sequence of characters that is used to represent text. You can do many thing with string like get substring from original string (with a condition), concate some strings, compare two strings, find a substring from original string… Bellow is example of declare and do some operations with string:
- //first string declaration
- String my_first_string = "This is declaration with string.";
- //second string declaration
- String my_second_string = " This is another declaration...";
- //get 10 charactes of original string, from zero character
- String sub = my_first_string.Substring(0, 10);
- //concatenate two strings
- String concated_string = String.Concat(my_first_string, my_second_string);
C# character Escape Sequence
Bellow table lists all character escape sequence when using string
Escape Sequence | Description |
\’ | Single quote, used for character literals. |
\" | Double quote, used for string literals. |
\\ | Backslash. |
\0 | Unicode character 0. |
\a | Alert (char 7). |
\b | Backspace (char 8). |
\f | Form Feed (char 12). |
\n | New Line (char 10). |
\r | Carriage Return (char 13). |
\t | Horizontal Tab (char 9). |
\v | Vertical Quote (char 11). |
\uxxxx | Unicode escape sequence for a character with a hexadecimal value xxxx. Also a variable-length version can contain 1 to 4 numeric values. |
\uxxxxxxxx | Unicode escape sequence for a character with a hexadecimal value of xxxxxxxx, used for generating Unicode surrogates. |
String formatting
String formatting is very important. For example, in some cases you want to print something like: “This number has value V” with V is dynamic change when you compute in your program. There is a very simple but efficient way to do with this case. C# provide some methods for string formatting. Table bellow list all string formatting methods in C#:
Method | Description |
String String.Format(String format , Object arg0 ) | Replaces the items in format string with the value of one Object |
String String.Format(String format , Object arg0 , Object arg1 ) | Replaces the items in format string with the value of two Objects |
String String.Format(String format , Object arg0 , Object arg1 , Object arg2 ) | Replaces the format items with the value of three Objects |
String String.Format(String format , params Object[] args ) | Replaces the format items with the value of array of Objects |
String String.Format(IFormatProvider provider , String format , params Object[] args ) | Replaces the format items with the value of array of Objects, but uses the specified culture-specific formatting information or custom formatting information. |
Let see example bellow to see how it works:
- static void Main(string[] args)
- {
- for (int i = 0; i < 5; i++)
- {
- so.printNumberVlue(i);
- }
- Console.ReadKey();
- }
- private void printNumberVlue(Int32 value)
- {
- Console.WriteLine("This number has value {0}", value);
- }
This is output of the example:
This number has value 0
This number has value 1
This number has value 2
This number has value 3
This number has value 4
In this example, the formatting method simply put the value of integer variable to the space “{0}” in the formatting string “This number has value {0}”.
Summary
This tutorial has expose to you some basic but very important information of what String type is and how to manipulate with String. In almost of program you will need this basic information to work with string and hope that this tutorial will be helpful to you.
Add new comment
- 102 views