Custom Search

Tuesday, March 25, 2008

Converting String to Int (C# .NET)

Here I will show you some easy ways you can convert String to integer in C#. The first way is by using the System namespace of the .NET Framework Class Library. In this namespace you can find the "Convert" class that lets you convert a base type to another base type.

String num = "123";

//First argument the String and second the base.
int a = System.Convert.ToInt32(num, 10);

Another way is by using the Parse function.

int b = Int32.Parse(num);

Now, if you want a method for doing these convertions, you can take a look at the following example. For argument the String value to be converted and return the integer for it. To make it a little interesting, the String value contains non numeric characters until the position number 6, example: abbton4457

public int change(String s) {
String st = s.Substring(6);
int x = Int16.Parse(st);
return x;

Monday, March 17, 2008

String Manipulation (C#)

Here I will show you some useful string manipulation functions in C#.

ToUpper, ToLower function.

Coverts all the characters from a String into upper/lower case. Example:

String st = " String Manipulation in C# ";
String a3 = st.ToUpper();

Trim function.

The trim function strips all white spaces form both start and end form a given string. It has two other variants, the TrimStart and TrimEnd.


String trimed = st.Trim();

Console.WriteLine("Original: " + st);
Console.WriteLine("Using Trim: " + "=" + trimed +"=");

String trimedStart = st.TrimStart();
Console.WriteLine("1st TrimStart: " + trimedStart + "=");

//Other TrimStart variant
char[] c = { ' ', 'S' };
String trimedStart2 = st.TrimStart(c);
Console.WriteLine("2nd TrimStart: " + trimedStart2 + "=");

ToReplace function.

The ToReplace function searches for a string/char to be replaced with a new string/char value. Example:

String toRepalce = st.Replace("C#" , "Java");
Console.WriteLine("=" + toRepalce + "=");

Split Function

Useful function when you want to split a string using delimiter. Example:

String [] a = st.Split('g');
String first = a[0];
String second = a[1];
Console.WriteLine("=" + first + "=");
Console.WriteLine("=" + second + "=");

Remove function.

The Remove function lets you strip a specified number of characters. The first value you set indicates the starting point and the second the total characters to strip. Example:

String a2 = st.Remove(10, 13);
Console.WriteLine("=" + a2 + "=");

Substring Function.

Removes a certain amount of characters form the 0 of the string to the used position. Example:

String a1 = st.Substring(9);
Console.WriteLine("=" + a1 + "=");

Wednesday, March 5, 2008

Structures (C#)

A Structure in C# is a composite data type containing many number of different data types. A Struct in C# can contain methods, constructors, fields, other structure type, properties, constants and indexers.

To declare a structure you use the struct keyword:

<modifiers> struct <struct_name>

For example:

public class Program

public struct St { //Could be private, internal or protected

public int val;
public String text;


public static void Main(string[] args)


//Initializing the struct

St s = new St();

//Adding values to the struct members

s.text = "Using struct";
s.val = 2;

Console.WriteLine("Value: " + s.val + " - Text: " + s.text);


Saturday, March 1, 2008

C# basics

In this video I found in Youtube you can learn how to write if statements, else if, boolean tests and more.