skip to main |
skip to sidebar
System.String provides a number of methods you would expect from such a utility class, including methods that return the length of the character data, find sub-strings within the current string,convert to and from uppercase/lowercase, and so forth. The String Type resides in the System namespace.All the string objects are mapped on the managed heap. Below is some String Functions Listed...//*****************************************************************************//Length This property returns the length of the current string.Compare() This method compares two strings.Contains() This method determines whether a string contains a specific substring.Equals() This method tests whether two string objects contain identical character data.Format() This method formats a string using other primitives (e.g., numerical data, other strings) and the {0} notation examined earlier in this chapter.Insert() This method inserts a string within a given string.PadLeft() This method is used to pad a string with some characters.PadRight()This method is used to pad a string with some characters.Remove() Use these methods to receive a copy of a string, with modifications (characters)Replace() removed or replaced).Split() This method returns a String array containing the substrings in this instance that are delimited by elements of a specified Char or String array.Trim() This method removes all occurrences of a set of specified characters from the beginning and end of the current string.ToUpper() These methods create a copy of the current string in uppercase or lowercaseToLower() format, respectively.//*****************************************************************************//
This "Parsing" technique can be extremely helpful when you wish to convert a bit of user input data (such as a selection from a GUI-based drop-down list box) into a numerical value. Consider the following parsing logic within a method named ParseFromStrings()://***********************************************************************//static void ParseFromStrings(){Console.WriteLine("=> Data type parsing:");bool b = bool.Parse("True");Console.WriteLine("Value of b: {0}", b);double d = double.Parse("99.884");Console.WriteLine("Value of d: {0}", d);int i = int.Parse("8");Console.WriteLine("Value of i: {0}", i);char c = Char.Parse("w");Console.WriteLine("Value of c: {0}", c);Console.WriteLine();}//***********************************************************************//OutPut:Value of b:TrueValue of d:99.84Value of i:8 Value of c:w
NOTE:The important thing to understand in this is Parse function becomes very important when you are working on GUI applications in which you have to take input from user in string format e.g.;
Enter Your Age : 21Enter Date: 11/04/2009In these scenarios to convert each data type to its actual type we use the method on Parsing,which is available for every data type(ValueType) present in C#.
All intrinsic data types support what is known as a default constructor.This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value:- bool types are set to false.
- Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
- char types are set to a single empty character.
- DateTime types are set to 1/1/0001 12:00:00 AM.
- Object references (including strings) are set to null.
The following is syntactically well-formed C# code://************************************************************************//
static void NewingDataTypes(){Console.WriteLine("=> Using new to create intrinsic data types:");bool b = new bool(); // Set to false.int i = new int(); // Set to 0.double d = new double(); // Set to 0.DateTime dt = new DateTime(); // Set to 1/1/0001 12:00:00 AMConsole.WriteLine("{0}, {1}, {2}, {3}", b, i, d, dt);Console.WriteLine();}//************************************************************************//
The Table below are the C# data type keywords which are present in System Namespace.***************************************************************************************Meaning in Life=
***************************************************************************************C#Shorthand= bool
System Type=System.Boolean
Meaning in Life=
Represents truth or falsityC#Shorthand=sbyte
System Type=System.SByte
Meaning in Life=
Signed 8-bit numberC#Shorthand=byte
System Type=System.Byte
Meaning in Life=
Unsigned 8-bit numberC#Shorthand=short
System Type=System.Int16
Meaning in Life=
Signed 16-bit numberC#Shorthand=ushort
System Type=System.UInt16
Meaning in Life=
Unsigned 16-bit numberC#Shorthand=int
System Type=System.Int32
Meaning in Life=
Signed 32-bit number C#Shorthand=uint
System Type=System.UInt32
Meaning in Life=
Unsigned 32-bit numberC#Shorthand=long
System Type=System.Int64
Meaning in Life=
Signed 64-bit numberC#Shorthand=ulong
System Type=System.UInt64
Meaning in Life=
Unsigned 64-bit numberC#Shorthand=char
System Type=System.Char
Meaning in Life=
Single 16-bit Unicode characterC#Shorthand=float
System Type=System.Single
Meaning in Life=
32-bit floating-point numberC#Shorthand=double
System Type=System.Double
Meaning in Life=
64-bit floating-point numberC#Shorthand=decimal
System Type=System.Decimal
Meaning in Life=
96-bit signed numberC#Shorthand=string
System Type=System.String
Meaning in Life=
Represents a set of Unicode charactersC#Shorthand=object
System Type=System.Object
Meaning in Life=
The base class of all types in the .NET universe***************************************************************************************NOTE:- Each of the numerical types (short, int, and so forth) map to a corresponding structure in the System namespace.
- In a nutshell, structures are "value types" allocated on the stack.
- On the other hand, string and object are "reference types," meaning the variable is allocated on the managed heap.
- Simply understand that value types can be allocated into memory very quickly and have a very fixed and predictable lifetime.
Here i have written a program that takes name and age as an input and displays it on the Console by changing the color of the text...//***********************************************************************************//using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Console.WriteLine("******Basic console I/O********"); GetUserData(); Console.ReadLine(); } static void GetUserData() { //Get Name and Age Console.Write("Please Enter Your Name"); string userName = Console.ReadLine(); Console.Write("Please Enter Your Age"); string userAge = Console.ReadLine(); //Change echo color,just for fun ConsoleColor prevColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; //Echo to the Console Console.WriteLine("Hello {0} You are {1} years old.",userName,userAge); //Restore Previous Color Console.ForegroundColor = prevColor; } } }//*******************************************************************************//InPut:
Enter Your Name Faisal
Enter Your Age OutPut:
Hello Faisal You are 21 Years Old.(color will be changed to green)
NOTE:
Here in this line one thing to notice is {0} and {1}...The question is why we are using it in Console.ReadLine()??The reason we are using is this that Console.ReadLine( ) works just like an array of strings.whatever you input first goes to the first index of Console.ReadLine() and so on...
Or we can say that all the inputs given from the Console is saved in a temporary array of strings,which we used in Console.WriteLine()
Remember that its index always start from 0,1,2,....You can also repeat the index {0} , {0} in Console.WriteLineConsole.WriteLine("{0},Number {0},Number{0}",9")
This class allows you to obtain a number of details regarding the operating system currently hosting your .Net Application using various static members.//**************************************************************//The program below is an Example Of Enviroment Class...//**************************************************************//using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { ShowEnviromentDetails(); //wait for enter key to be presses before quit Console.ReadLine(); } static void ShowEnviromentDetails() { //Print Out the drives on this machine and other interesting details foreach (string drive in Environment.GetLogicalDrives()) Console.WriteLine("Drive:{0}", drive); Console.WriteLine("OS:{0}", Environment.OSVersion); Console.WriteLine("Number of processors:{0}",Environment.ProcessorCount); Console.WriteLine(".Net Version:{0}",Environment.Version); } }}//**************************************************************////**************************************//Select Properties of System.Enviroment//**************************************//**********************************Property and its Meaning in Life**********************************Exit Code Gets or sets the exit code any where within the application Machine Name Gets the name of the current machine NewLine Gets the new line symbol for the current enviromentStackTrace Gets the current stack trace information for the applicationSystem Directory Returns the full path to the system directoryUser Name Returns the name of user that started this application//*********************************************************************************//You can use more Enviroment variables by using the intellisence...just like thisConsole.WriteLine(Environment.(Here a list of properties will appear in front of you) );Do Check this for your practice...
//******************************************//Three are three ways to define Main() in C#//******************************************////int return type,array of strings as the argumentstatic int Main( string[] args){}//No return type,No argumentstatic void Main( ){}//int return type,no argumentstatic int Main(){}