Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C#

Learn step by step



String

A string is basically a sequence of characters. Each character is a Unicode character in the range U+0000 to U+FFFF (more on that later). The string type (I'll use the C# shorthand rather than putting System.String each time) has the following characteristics:

Declaring and Initializing Strings
// Declare without initializing.
string message1;

// Initialize to null.
string message2 = null;

// Initialize as an empty string.
// Use the Empty constant instead of the literal "".
string message3 = System.String.Empty;

//Initialize with a regular string literal.
string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";

// Initialize with a verbatim string literal.
string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0";

// Use System.String if you prefer.
System.String greeting = "Hello World!";

// In local variables (i.e. within a method body)
// you can use implicit typing.
var temp = "I'm still a strongly-typed System.String!";

// Use a const string to prevent 'message4' from
// being used to store another string value.
const string message4 = "You can't get rid of me!";

// Use the String constructor only when creating
// a string from a char*, char[], or sbyte*. See
// System.String documentation for details.
char[] letters = { 'A', 'B', 'C' };
string alphabet = new string(letters);
Methods of the String Class

S.NMethod Name & Description
1public static int Compare( string strA, string strB )
Compares two specified string objects and returns an integer that indicates their relative position in the sort order.
2public static int Compare( string strA, string strB, bool ignoreCase )
Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true.
3public static string Concat( string str0, string str1 )
Concatenates two string objects.
4public static string Concat( string str0, string str1, string str2 )
Concatenates three string objects.
5public static string Concat( string str0, string str1, string str2, string str3 )
Concatenates four string objects.
6public bool Contains( string value )
Returns a value indicating whether the specified string object occurs within this string.
7public static string Copy( string str )
Creates a new String object with the same value as the specified string.
8public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count )
Copies a specified number of characters from a specified position of the string object to a specified position in an array of Unicode characters.
9public bool EndsWith( string value )
Determines whether the end of the string object matches the specified string.
10public bool Equals( string value )
Determines whether the current string object and the specified string object have the same value.
11public static bool Equals( string a, string b )
Determines whether two specified string objects have the same value.
12public static string Format( string format, Object arg0 )
Replaces one or more format items in a specified string with the string representation of a specified object.
13public int IndexOf( char value )
Returns the zero-based index of the first occurrence of the specified Unicode character in the current string.
14public int IndexOf( string value )
Returns the zero-based index of the first occurrence of the specified string in this instance.
15public int IndexOf( char value, int startIndex )
Returns the zero-based index of the first occurrence of the specified Unicode character in this string, starting search at the specified character position.
16public int IndexOf( string value, int startIndex )
Returns the zero-based index of the first occurrence of the specified string in this instance, starting search at the specified character position.
17public int IndexOfAny( char[] anyOf )
Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
18public int IndexOfAny( char[] anyOf, int startIndex )
Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters, starting search at the specified character position.
19public string Insert( int startIndex, string value )
Returns a new string in which a specified string is inserted at a specified index position in the current string object.
20public static bool IsNullOrEmpty( string value )
Indicates whether the specified string is null or an Empty string.
21public static string Join( string separator, params string[] value )
Concatenates all the elements of a string array, using the specified separator between each element.
22public static string Join( string separator, string[] value, int startIndex, int count )
Concatenates the specified elements of a string array, using the specified separator between each element.
23public int LastIndexOf( char value )
Returns the zero-based index position of the last occurrence of the specified Unicode character within the current string object.
24public int LastIndexOf( string value )
Returns the zero-based index position of the last occurrence of a specified string within the current string object.
25public string Remove( int startIndex )
Removes all the characters in the current instance, beginning at a specified position and continuing through the last position, and returns the string.
26public string Remove( int startIndex, int count )
Removes the specified number of characters in the current string beginning at a specified position and returns the string.
27public string Replace( char oldChar, char newChar )
Replaces all occurrences of a specified Unicode character in the current string object with the specified Unicode character and returns the new string.
28public string Replace( string oldValue, string newValue )
Replaces all occurrences of a specified string in the current string object with the specified string and returns the new string.
29public string[] Split( params char[] separator )
Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array.
30public string[] Split( char[] separator, int count )
Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. The int parameter specifies the maximum number of substrings to return.
31public bool StartsWith( string value )
Determines whether the beginning of this string instance matches the specified string.
32public char[] ToCharArray()
Returns a Unicode character array with all the characters in the current string object.
33public char[] ToCharArray( int startIndex, int length )
Returns a Unicode character array with all the characters in the current string object, starting from the specified index and up to the specified length.
34public string ToLower()
Returns a copy of this string converted to lowercase.
35public string ToUpper()
Returns a copy of this string converted to uppercase.
36public string Trim()
Removes all leading and trailing white-space characters from the current String object.



Related Videos