ankitweblogic logo
W e b t u t o r i a l s

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators


Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection


C#.Net Strings

A String is a collection of characters. In C#.Net, string is a reference data type. The benefit of reference type is that it can reduce or increase the size.

String Methods are:
1. Length - Calculate the number of characters and return an integer value.
Syntax: int len = string_variable.Length;

2. Split() - Use to split a string by specifying delimiter character and return array of string.
Syntax: string[] s = a.Split (' ');

3. Substring() - Use to retrieve a substring from a string starting from a specified character position. You can also specify the length.
string s = a.Substring(5); //return characters from 5th index to end of the list.
string s2 = a.Substring(5,3); //return 3 characters from 5th index to 8th index.

4. StrReverse() - Use to reverse the characters in the string.
string s = "cab";
char[] c = s.ToCharArray();

5. Equals() - Check two strings for their equality and returns a boolean value. Returns True, if both strings are equal otherwise return false.
Syntax: s1.Equals(s2)

6. Compare() or CompareTo
Compare two strings and return a number. If the number is zero means both strings are equal, if the number is greater than zero means first string is greater and if the number is less than zero means second string is greater.
Syntax: int x = string.Compare(a, b);
You can also specify the third parameter that is for ignore cases, True for ignore cases otherwise false (default).
Syntax: int x = string.Compare(a, b, true);
Syntax: int x = a.CompareTo(b);

7. ToCharArray() - Use to convert a String into Array of characters.
Syntax: char[] arr = a.ToCharArray();

8. ToLower() - Use to convert a string characters into lower case letters
Syntax: string x = a.ToLower();

9. ToUpper() - Use to convert a string characters into upper case letters
Syntax: string x = a.ToUpper ();

10. IndexOf() - Search a character or a string and return its index position.
Syntax: int c = a.IndexOf('a');

11. Insert() - Use to insert text at a specified index location of a string. We can insert either a character or a string at a given index location.
Syntax:string c = a.Insert(5, "!");

12. Replace() - Removes characters from a string and replaces them with a new character or string.
Syntax: string c = a.Replace(",", "!!");

13. Remove() - Use to delete specified number of characters from the location specified till end of the string.
string c = a.Remove(5); // delete from 5th character to end of the list.
However you can also specify the number of characters.
string c = a.Remove(5,2); // delete 2 characters starting from 5th position.

14. Copy() - Use to copy a string into another string. It is similar to assigning a string to another string.
Syntax: string c = string.Copy(a); or string c = a;

15. Trim() - Use to remove leading and trailing spaces as well as to remove any unwanted character.
Syntax: string c = a.Trim('');
string c = a.Trim(' # '); //Remove leading and trailing # character.

16. PadLeft() - Use to fill a character in the beginning of the output screen if the number of total width specified is greater than the length of a string.
Syntax: string c = a.PadLeft(20,'*');

17. PadRight() - Same as PadLeft instead it fills character on the right of a string.

18. StartsWith() - Find a string is starting with a specified string or not, return a Boolean value.
Syntax: bool c = a.StartsWith("Hey");

19. EndsWith() - Same as StartsWith(), only difference is that it search a string from end.

20. Concat() - Use to concatenate strings, you can use ‘+’ operator also.
Syntax: string c = string.Concat(a, b);

21. Contains() - Returns a boolean value indicating whether the specified String object occurs within a string.
Syntax: bool b = s.Contains("a");

22. Tryparse() - Use to check if the string contains integer value or not, it returns a Boolean value. Tryparse checks for a valid range.
Syntax: int i;
bool c = Int32.TryParse(a, out i);

Updated: 02-Feb-19