C# - Strings: Creating A String Object
C# - Strings: Creating A String Object
In C#, you can use string s as array of characters, however, more common practice is to use the string keyword
to declare a string variable. T he string keyword is an alias for the System.String class.
When the above code is compiled and executed, it produces the following result:
Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
S.N
Chars
Gets the Char object at a specified position in the current String object.
Leng th
Gets the number of characters in the current String object.
S.N
public static int Compare( string strA, string strB, bool ig noreCase )
Compares two specified string objects and returns an integ er that indicates their relative position in
the sort order. However, it ig nores case if the Boolean parameter is true.
public static string Conc at( string str0, string str1, string str2 )
Concatenates three string objects.
public static string Conc at( string str0, string str1, string str2, string str3 )
Concatenates four string objects.
public void CopyT o( int sourc eIndex, c har[] destination, int destinationIndex, int
c ount )
Copies a specified number of characters from a specified position of the string object to a specified
position in an array of Unicode characters.
10
11
12
13
string .
14
15
16
17
18
19
20
21
22
public static string J oin( string separator, string [] value, int startIndex, int c ount )
Concatenates the specified elements of a string array, using the specified separator between each
element.
23
24
25
26
27
28
29
30
Returns a string array that contains the substring s in the current string object, delimited by elements
of a specified Unicode character array. T he int parameter specifies the maximum number of
substring s to return.
31
32
33
34
35
36
T he above list of methods is not exhaustive, please visit MSDN library for the complete list of methods and String
class constructors.
Examples:
T he following example demonstrates some of the methods mentioned above:
Comparing String s:
using System;
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0)
{
Console.WriteLine(str1 + " and " + str2 + " are equal.");
}
else
{
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
When the above code is compiled and executed, it produces the following result:
This is test and This is text are not equal.
class StringProg
{
static void Main(string[] args)
{
string str = "This is test";
if (str.Contains("test"))
{
Console.WriteLine("The sequence 'test' was found.");
}
Console.ReadKey() ;
}
}
}
When the above code is compiled and executed, it produces the following result:
The sequence 'test' was found.
Getting a Substring :
using System;
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str = "Last night I dreamt of San Pedro";
Console.WriteLine(str);
string substr = str.Substring(23);
Console.WriteLine(substr);
}
Console.ReadKey() ;
}
}
When the above code is compiled and executed, it produces the following result:
San Pedro
J oining String s:
using System;
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
}
Console.ReadKey() ;
}
}
When the above code is compiled and executed, it produces the following result:
Down the way nights are dark
And the sun shines daily on the mountain top