String creation

In this chapter you will learn:

  1. Create string from characters
  2. Create string from character array

Create string from characters

The following code creates a repeating sequence of a certain char.

using System;/*from   ja v a 2s .  c  o m*/

class Sample
{
    public static void Main()
    {
        string s = new string('a', 5);
        Console.WriteLine(s);


    }
}

The output:

Create string from character array

The following code creates string from character array.

using System;/*  j a v  a2s.co  m*/

class Sample
{
    public static void Main()
    {
        char[] charArray = new char[] { 'j', 'a', 'v', 'a', 
                            '2', 's', '.', 'c', 'o', 'm', };

        string s = new string(charArray);

        Console.WriteLine(s);

    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Display all the characters in myString using a for loop
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String