Advertisement
String str1 = "Hello There";
//Creating a char array
char[] arr = {'H', 'e', 'y', ' ', 'T', 'h', 'e', 'r', 'e'};
//Creating a String object from a char[] array.
String str2 = new String(arr);
//Creating two String objects from String literals.
String s1 = "Hello ";
String s2 = "You";
//Creating new String object by using + operator
String s3 = s1 + s2;
Advertisement
//C# Creating and initializing String objects in different ways.
using System;
class String1
{
public static void Main()
{
//Creating a char array
char[] arr = {'N', 'e', 'w', ' ', 'Y', 'o', 'r', 'k'};
//Creating the first String object from a char[] array.
String str1 = new String(arr);
//Second String object
String str2 = "New Delhi";
//Third String object.
String str3 = "London";
//Printing the String objects
Console.WriteLine("String 1 : " + str1);
Console.WriteLine("String 2 : " + str2);
Console.WriteLine("String 3 : " + str3);
//Creating a new String by adding two String objects by + operator
String str4 = str1+str2;
Console.WriteLine("String 4 : "+ str4);
}
}
String 1 : New York
String 2 : New Delhi
String 3 : London
String 4 : New YorkNew Delhi
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement