.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Some string operations

Posted By:SanthoshNathan       Posted Date: November 01, 2010    Points: 15    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Some string operations

using System;  
public class StrOps {   
  public static void Main() {   
    string str1 =  
      "When it comes to .NET programming, C# is #1.";   
    string str2 = string.Copy(str1);  
    string str3 = "C# strings are powerful.";   
    string strUp, strLow; 
    int result, idx;  
    Console.WriteLine("str1: " + str1)
    Console.WriteLine("Length of str1: " +   
    // create upper- and lowercase versions of str1  
    strLow = str1.ToLower()
    strUp =  str1.ToUpper()
    Console.WriteLine("Lowercase version of str1:\n    " 
    Console.WriteLine("Uppercase version of str1:\n    " 
    // display str1, one char at a time.  
    Console.WriteLine("Display str1, one char at a time.")
    for(int i=0; i < str1.Length; i++)  
    // compare strings 
    if(str1 == str2)   
      Console.WriteLine("str1 == str2");   
      Console.WriteLine("str1 != str2");   
    if(str1 == str3)   
      Console.WriteLine("str1 == str3");   
      Console.WriteLine("str1 != str3");   
    result = str1.CompareTo(str3);  
    if(result == 0)  
      Console.WriteLine("str1 and str3 are equal");  
    else if(result < 0)  
      Console.WriteLine("str1 is less than str3");  
      Console.WriteLine("str1 is greater than str3");  
    // assign a new string to str2  
    str2 = "One Two Three One";  
    // search string 
    idx = str2.IndexOf("One");  
    Console.WriteLine("Index of first occurrence of One: " + idx);  
    idx = str2.LastIndexOf("One");  
    Console.WriteLine("Index of last occurrence of One: " + idx);  


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend