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:

  1. //first string declaration
  2. String my_first_string = "This is declaration with string.";
  3.  
  4. //second string declaration
  5. String my_second_string = " This is another declaration...";
  6.  
  7. //get 10 charactes of original string, from zero character
  8. String sub = my_first_string.Substring(0, 10);
  9.  
  10. //concatenate two strings
  11. 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 SequenceDescription
\’Single quote, used for character literals.
\"Double quote, used for string literals.
\\Backslash.
\0Unicode 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#:

MethodDescription
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:

  1. static void Main(string[] args)
  2. {
  3. for (int i = 0; i < 5; i++)
  4. {
  5. StringOverview so = new StringOverview();
  6. so.printNumberVlue(i);
  7. }
  8. Console.ReadKey();
  9. }
  10.  
  11. private void printNumberVlue(Int32 value)
  12. {
  13. Console.WriteLine("This number has value {0}", value);
  14.  
  15. }

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