Friday 24 August 2012

Substring() in C#

Example On String Substring() method.


Substring in C# string Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to the given length.

Syntax : string string.substring(int startIndex,int length)


Parameters:

startIndex: The index of the start of the substring.
length: The number of characters in the substring.



public class SubstringExample
      {
            public static void Main()
            {                
                  string str = null;
                  string SubStr = null;
                  str = "Dotnet Masty Substring Example";
                  SubStr = str.Substring(7, 15);

                  Console.WriteLine(SubStr);               
                  Console.ReadLine();
            }
      }

Thursday 23 August 2012

String Copy() and CopyTo() in C#

Example on String Copy() and CopyTo() Method.

Copy() :

CSharp String Copy method is create a new String object with the same content.
Syntax: string string.Copy(string str)


Parameters:
String str : The argument String for Copy method.




CopyTo() :

CSharp string CopyTo method Copies a specified number of characters from a specified position in this instance to a specified position in an array of characters.

Syntax: void string.CopyTo(int sourceIndex,char[] destination,  int destinationindex,int count)

Parameters:
int sourceIndex : The starting position of the source String
char[] destination : The character Array
int destinationindex : Array element in the destination
int count : The number of characters to destination


public class StringCopy&CopyToExample
      {
            public static void Main()
            {
                  //Copy() Methode
                  string strCopy1 = null;
                  string strCopy2 = null;

                  strCopy1 = "C# Copy() Method test";
                  strCopy2 = string.Copy(strCopy1);
                  Console.WriteLine(strCopy2);
                  //MessageBox.Show(str2);
                 
                  //CopyTo() Methode
                  string strTo = "C#CopyTo() sample";
                  char[] chrs = new char[5];
                  strTo.CopyTo(0, chrs, 0, 5);
                 
                  Console.WriteLine(chrs[0].ToString() + chrs[1].ToString() + chrs[2].ToString()+ chrs[3].ToString()  + chrs[4].ToString() + chrs[5].ToString());
           
                  Console.ReadLine();
            }
      }

String Clone() in C#

Example on String Clone() Method.


The C# String Clone() method creates and returns a copy of string object. This methode returns a reference to this instance of string.


public class StringCloneExample
      {
            public static void Main()
            {
                  string Str = "Clone Methode Testing";
                  string StrCloned = null;
                  StrCloned = (String)Str.Clone();
                  Console.WriteLine(StrCloned);
                  Console.ReadLine();
            }
      }

String Split in C#

Example on Split String using C#



C# string Split function returns an array of String containing the substrings delimited by the given System.Char array.



public class StringSplitExample
      {
            public static void Main()
            {
                  string str = null;
                  string[] strArr = null;
                  int count = 0;
                  str = "Dot Net Masty";
                  char[] splitchar = { ' ' };
                  strArr = str.Split(splitchar);
                  for (count = 0; count <= strArr.Length - 1; count++)
                  {
                        Console.WriteLine(strArr[count]);                    
                  }
                  Console.ReadLine();
            }
      }

String.Compare Methods in C#

Different types of String Compare Methods Examples 

public class StringCompareExample
      {
            public static void Main()
            {
                  string s1 = "Dot\u00ADNet"; // '\u00AD' Unicode Character 'SOFT HYPHEN'
                  string s2 = "DotNet";                    
                  Console.WriteLine("Comparison of '{0}' and '{1}': {2}", s1, s2, String.Compare(s1, s2, true));           
                 
                  Console.WriteLine("2nd Example : ");
                  String MyString = "Hello World1";
                  String OtherString = "Hello World";
                  int MyInt = MyString.CompareTo(OtherString);
                  Console.WriteLine( MyInt );
                 
                  Console.WriteLine("3rd Example : ");
                  string MyString1 = "Hello World";
                  string YourString = "Hello World1";
                  Console.WriteLine(String.Equals(MyString1, YourString));

                  Console.WriteLine("4th Example : ");
                  string MyString2 = "Hello World";
                  Console.WriteLine(MyString2.StartsWith("Hello"));
                 
                  // String.Compare Methods
                  Console.WriteLine("5th Example : ");
                  string str1 = null;
                  string str2 = null;
                 
                  str1 = "csharp";
                  str2 = "CSharp";
                 
                  int result = 0;
                 
                  result = string.Compare(str1, str2);
                  Console.WriteLine(result.ToString());                
                 
                  result = string.Compare(str1, str2, true);                 
                  Console.WriteLine(result.ToString());
                 
                  result = string.Compare(str1, str2, false);                
                  Console.WriteLine(result.ToString());
                 
                  Console.ReadLine();
            }
      }