• We just launched and are currently in beta. Join us as we build and grow the community.

String formatting in C# Language

ozyx.g

Malware Analyst
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
74
Likes
98
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. //second string declaration
  4. String

    my_second_string =

    " This is another declaration..."

    ;

  5. //get 10 charactes of original string, from zero character
  6. String

    sub =

    my_first_string.

    Substring

    (

    0

    , 10

    )

    ;

  7. //concatenate two strings
  8. 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:

  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. private

    void

    printNumberVlue(

    Int32 value

    )
  11. {
  12. Console.

    WriteLine

    (

    "This number has value {0}"

    , value

    )

    ;

  13. }

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.

Book traversal links for String formatting in C# Language

  • ‹ Stack and Queue in C#
  • Up
  • Using Enumeration in C# ›

 

439,009

316,008

316,017

Top