String Methods/Functions

In C# programming, string is another kind of data type that represents Unicode Characters. It is the alias of System.String however, you can also write System.String instead of string. It is the sequence of character in which each character is a Unicode character.

There is no difference between string and String because string is the alias of System.String. Most of the developers get confused what to use between sting and String. Technically there is no difference between them and they can use any of them. However, you will have to use “using System” to use the String in C#. Another difference is String is a class name whereas string is a reserved keyword. You should always use string instead of String.

Here are definitions for methods and you can click on method to see example;

STRING FUNCTIONS/METHODS DEFINITIONS
Clone() Make clone of string.
CompareTo() Compare two strings and returns integer value as output. It returns 0 for true and 1 for false.
Contains() The C# Contains method checks whether specified character or string is exists or not in the string value.
EndsWith() This EndsWith Method checks whether specified character is the last character of string or not.
Equals() The Equals Method in C# compares two string and returns Boolean value as output.
GetHashCode() This method returns HashValue of specified string.
GetType() It returns the System.Type of current instance.
GetTypeCode() It returns the Stystem.TypeCode for class System.String.
IndexOf() Returns the index position of first occurrence of specified character.
ToLower() Converts String into lower case based on rules of the current culture.
ToUpper() Converts String into Upper case based on rules of the current culture.
Insert() Insert the string or character in the string at the specified position.
IsNormalized() This method checks whether this string is in Unicode normalization form C.
LastIndexOf() Returns the index position of last occurrence of specified character.
Length It is a string property that returns length of string.
Remove() This method deletes all the characters from beginning to specified index position.
Replace() This method replaces the character.
Split() This method splits the string based on specified value.
StartsWith() It checks whether the first character of string is same as specified character.
Substring() This method returns substring.
ToCharArray() Converts string into char array.
Trim() It removes extra whitespaces from beginning and ending of string.

 

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

   class Program

   {

   static void Main(string[] args)

     {

       string firstname;

       string lastname;

       firstname = "Steven Clark";

       lastname = "Clark";

   // Make String Clone    

   Console.WriteLine("1-(Clone example): " + firstname.Clone());

   Console.WriteLine("");

   //Compare two string value and returns 0 for true and 1 for false

   Console.WriteLine("2-(CompareTo example): " + firstname.CompareTo(lastname));

   Console.WriteLine("");

   //Check whether specified value exists or not in string

   Console.WriteLine("3-(Contains example): " + firstname.Contains("ven"));

   Console.WriteLine("");

   //Check whether specified value is the last character of string

   Console.WriteLine("4-(EndsWith example): " + firstname.EndsWith("n"));

   Console.WriteLine("");

   //Compare two string and returns true and false

   Console.WriteLine("5-(Equals example): " + firstname.Equals(lastname));

   Console.WriteLine("");

   //Returns HashCode of String

   Console.WriteLine("6-(GetHashCode example): " + firstname.GetHashCode());

   Console.WriteLine("");

   //Returns type of string

   Console.WriteLine("6-(GetType example): " + firstname.GetType());

   Console.WriteLine("");

   //Returns type of string

   Console.WriteLine("7-(GetTypeCode example): " + firstname.GetTypeCode());

   Console.WriteLine("");

   //Returns the first index position of specified value the first index position of specified value

   Console.WriteLine("8-(IndexOf example): " + firstname.IndexOf("e"));

   Console.WriteLine("");

   //Covert string into lower case

   Console.WriteLine("9-(ToLower example): " + firstname.ToLower());

   Console.WriteLine("");

   Console.WriteLine("10-(ToUpper example): " + firstname.ToUpper());

   Console.WriteLine("");

   //Convert string into Upper case

   Console.WriteLine("11-(Insert example): " + firstname.Insert(0, "Hello")); //Insert substring into string

   Console.WriteLine("");

   Console.WriteLine("12-(IsNormalized example): " + firstname.IsNormalized());

   Console.WriteLine("");

   //Check Whether string is in Unicode normalization from C

   //Returns the last index position of specified value

   Console.WriteLine("13-(LastIndexOf example): " + firstname.LastIndexOf("e"));  

     Console.WriteLine("");

   //Returns the Length of String

     Console.WriteLine("14-(Length example): " + firstname.Length);

   Console.WriteLine("");

   //Deletes all the characters from begining to specified index.

   Console.WriteLine("15-(Remove example): " + firstname.Remove(5));

   Console.WriteLine("");

   // Replace the character

   Console.WriteLine("16-(Replace example): " + firstname.Replace('e', 'i'));

   Console.WriteLine("");

   //Split the string based on specified value

   string[] split = firstname.Split(new char[] { 'e' });

   Console.WriteLine("17-(split example): " + split[0]);

   Console.WriteLine("17-(split example): " + split[1]);

   Console.WriteLine("17-(split example): " + split[2]);

   //Check wheter first character of string is same as specified value

   Console.WriteLine("18-(split example): " + firstname.StartsWith("S"));

   Console.WriteLine("");

   //Returns substring

   Console.WriteLine("19-(split example): " + firstname.Substring(2, 5));

   Console.WriteLine("");

   //Converts an string into char array.

   Console.WriteLine("20-(split example): " + firstname.ToCharArray());

   Console.WriteLine("");

//It removes starting and ending white spaces from string.

   Console.WriteLine("21-(split example): " + firstname.Trim());

   Console.WriteLine("");

   Console.ReadLine();

     }

   }

}

Source:

http://www.completecsharptutorial.com/articles/csharp-string-function.php

http://msdn.microsoft.com/en-us/library/system.string_methods(v=vs.71).aspx

http://newsbilisim.blogcu.com/string-uzerinde-islem-yapan-metodlar/3976006

 

İlk yorum yapan olun

Bir yanıt bırakın

E-posta hesabınız yayımlanmayacak.


*