C Sharp/introtostrings

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control
C# - Variables II

C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

Contents

[edit] Basic C# string

We have used a very basic form of strings in C#. Now we will discuss string manipulation and some basic string methods that are at your disposal. C# string class is very powerful. There not many situations where you won't need C#'s string class.


C# strings are simply an array of characters. Remember, characters can only contain one letter so strings so to speak is an extension of the char class. Lets look at some examples:


Declare a character:

char x = 'a';       //One character
char[] MyName = { 'T', 'o', 'b', 'y' };   //array of characters

Declare a string:

string x = "csharp";    //One string
string[] x = {"one", "two", "three", "four"};  //array of strings


Convert char array to string and vice versa:

char[] MyCharArray = DummyString.ToCharArray();  //string to char array
string DummyString = new string(MyCharArray);    //char array to string


C# code example:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        char[] MyName = { 'T', 'o', 'b', 'y' };   //array of characters
        string Mynamestring = new string(MyName);
 
        //use for loop to print MyName
        for (int x = 0; x < MyName.GetLength(0); ++x)
            Console.Write(MyName[x]);           //Print each character
 
        Console.WriteLine("");                  //Print new line
        Console.WriteLine(Mynamestring);
        Console.Read();
    }
}

Output:

Toby
Toby


[edit] Change case in C#

A simple program to change C# case:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        string x = "Janene";
 
        Console.WriteLine(x);
 
        x = x.ToLower();
        Console.WriteLine(x);
 
        x = x.ToUpper();
        Console.WriteLine(x);
 
        Console.Read();
    }
}

Output:

Janene
janene
JANENE


[edit] C# String Concatenation

A simple program to combine two or more strings

using System;
 
class Program
{
    static void Main(string[] args)
    {
        string x = "My name is ";
        string y = "Devan";
 
        string z = x + y;
        string n = "My name is " + y;
 
        Console.WriteLine(z);
        Console.WriteLine(n);
 
        Console.Read();
    }
}


[edit] C# string manipulation

[edit] C# Trim method

Characters can be deleted from within a string by using the trim method. This is how you would use it

  • put unwanted characters in a char array if you have multiple characters to delete
  • Trim method is case sensitive
  • Trim only deletes characters from beginning or end of string
  • Trim will not delete characters in the middle of a string

C# code example:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        char[] DeleteChars = {':', ' '};
        string MyName = " Charles:";
 
        string OriginalString = MyName;
        MyName = MyName.Trim(DeleteChars);
 
        Console.WriteLine(OriginalString);
        Console.WriteLine(MyName);
        Console.Read();
    }
}

Output:

  Charles:
 Charles


You can also use TrimStart and TrimEnd which will trim characters from the start or end of a string.

[edit] C# String padding

PadRight() and PadLeft() are used to pad strings with choosen and amount of characters.

Example:

string x = "A String";
MyName = MyName.PadLeft(8, '-');

The code above simply pads the string with one - character. Remember padding works from integers from 8 and up. If you use the code MyName.PadLeft(7, '-'); it will not work. You must start from 8. PadRight is the same as PadLeft except that it works in the opposite direction.


using System;
 
class Program
{
    static void Main(string[] args)
    {
        string MyName = "Charles";
        MyName = MyName.PadLeft(8, '-');
 
        Console.WriteLine(MyName);
        Console.Read();
    }
}

Output:

-Charles

[edit] C# String Tokens

C# has a method to tokenize or split strings into an array of strings. It uses a delimeter to do this. You must specify a character or array of characters to tell the compiler how to split the strings.


Example:

using System;
 
class Program
{
    static void Main(string[] args)
    {
        string MyString = "I am learning C#";
 
        //Use a blank space as delimeter
        string[] MyStringTokens = MyString.Split(' ');  
 
        Console.WriteLine(MyString);
        Console.WriteLine("");
        foreach (string tempstring in MyStringTokens)
            Console.WriteLine(tempstring);
 
        Console.Read();
    }
}