Showing posts with label Basics Of C#. Show all posts
Showing posts with label Basics Of C#. Show all posts

Monday, February 27, 2012

Upper case, Lower case, Proper Case or Title Case in C#


Convert a string to Uppercase

String Sub_Code = txtcode.Text.ToUpper();

Convert a string to Lowercase

String Sub_Code = txtcode.Text.ToLower();

Note : ToLower and ToUpper are methods of string class.

Convert a string to ProperCase or TitleCase
The String class does not include any method that converts a string to title case. To convert  a string to title case we use ToTitleCase method which resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.

When we use the TextInfo class, we must specify cultural information. CultureInfo class represents information about a specific culture including the names of the culture, the writing system, and the calendar used, as well as access to culture-specific objects that provide information for common operations, such as formatting dates and sorting strings.

Example :
CultureInfo cultureinfo = CultureInfo.InvariantCulture;
TextInfo textinfo = cultureinfo.TextInfo;

string Sub_Name =textinfo.ToTitleCase(txtname.Text);

 

Monday, February 20, 2012

Substring Function

For any function there arises three basic question--:
  1. What does it mean?
  2. Why is this?
  3. How it works?
What does it mean?
As we see SubString is a combination of two words "Sub" and "String".
String-: In Computer Science String  means "A set of consecutive characters".
Sub   -: Sub is basically a Latin prefix which has several meaning but the best suitable meaning for here is "part of".
So, Substring means "Part of a string".

Why is this?
So, from definition its clear that the work of substring is to extract a part of string.

How it works? 
Syntax : Substring(index,length)
            Here index denotes the postion from where you want to extract the string and length denotes up to how much character you need to extract.Both contains integer value.
Example :
        int ind = Convert.ToInt32(txtIndex.Text);
        int len = Convert.ToInt32(txtLength.Text);
        Literal1.Text = TextBox1.Text.Substring(ind,len); 


You can download Sample Code from Here