Video Games Development

CSharp – Strings

CSharp – Strings

In C#, you can use strings as:
– string keyword to declare a string variable (alias for the System.String class)
– array of characters

How you can use string


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //from string literal and string concatenation
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";

            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);

            //by using string constructor
            char[] letters = { 'H', 'e', 'l', 'l', 'o' };
            string greetings = new string(letters);
            Console.WriteLine("Greetings: {0}", greetings);

            //methods returning string
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);

            //formatting method to convert a value 
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}",
            waiting);
            Console.WriteLine("Message: {0}", chat);
            Console.ReadKey();
        }
    }
}

The result is:

Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012

Comparing Strings


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class StringProg
    {
        static void Main(string[] args)
        {
            string str1 = "This is test";
            string str2 = "This is text";

            if (String.Compare(str1, str2) == 0)
            {
                Console.WriteLine(str1 + " and " + str2 + " are equal.");
            }
            else
            {
                Console.WriteLine(str1 + " and " + str2 + " are not equal.");
            }
            Console.ReadKey();
        }
    }
}

The result is:

This is test and This is text are not equal.

Detect a string


using System;

namespace ConsoleApplication1
{
    class StringProg
    {
        static void Main(string[] args)
        {
            string str = "This is test";
            if (str.Contains("test"))
            {
                Console.WriteLine("The sequence 'test' was found.");
            }
            Console.ReadKey();
        }
    }
}

The result is:

The sequence ‘test’ was found.

Get a substring


using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "Last night I dreamt of San Pedro";
         Console.WriteLine(str);
         string substr = str.Substring(23); // from 24th character to the end
         Console.WriteLine(substr);
      }
      Console.ReadKey() ;
   }
}

The result is:

San Pedro

Joining Strings


using System;

namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string[] starray = new string[]{"Down the way nights are dark",
         "And the sun shines daily on the mountain top",
         "I took a trip on a sailing ship",
         "And when I reached Jamaica",
         "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
      Console.ReadKey() ;
   }
}

The result is:

Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop

My website: http://www.lucedigitale.com

Ref: http://www.tutorialspoint.com/csharp/csharp_strings.htm

By |CSharp|Commenti disabilitati su CSharp – Strings

CSharp – for Statement

CSharp – for Statement


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            for(int i = 0; i < number; i++)
                Console.WriteLine(i);

            // the console will close if you press Return on the keyboard
            Console.ReadLine();
        }
    }
}

The result is:
0
1
2
3
4

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – for Statement

CSharp – while Statement

CSharp – while Statement


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;

            while(number < 5)
            {
                Console.WriteLine(number);
                number = number + 1;
            }

	    // the console will close if you press Return on the keyboard
            Console.ReadLine();
        }
    }
}

The result is:
0
1
2
3
4

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – while Statement

CSharp – Switch Statement

CSharp – Switch Statement

The switch statement is like a set of if statements. It’s a list of possibilities, with an action for each possibility, and an optional default action, in case nothing else evaluates to true.

switch case


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int caseSwitch = 1;
            switch (caseSwitch)
            {
                case 1:
                    Console.WriteLine("Case 1");
                    break;
                case 2:
                    Console.WriteLine("Case 2");
                    break;
                // if case 1 and case 2 are false:
                default:
                    Console.WriteLine("Default case");
                    break;
            }
        } // End Main()
    }
}

switch case – while

How to create a loop:

...
case 4:
    while (true)
        Console.WriteLine("Endless looping. . . .");
...
By |CSharp, Video Games Development|Commenti disabilitati su CSharp – Switch Statement

CSharp – if Statement

CSharp – if Statement

With if statement you can set up conditional blocks of code.

if – else if – else


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char ch = (char)Console.Read();

            if (Char.IsUpper(ch))
            {
                Console.WriteLine("The character is an uppercase letter.");
            }
            else if (Char.IsLower(ch))
            {
                Console.WriteLine("The character is a lowercase letter.");
            }
            else if (Char.IsDigit(ch))
            {
                Console.WriteLine("The character is a number.");
            }
            else
            {
                Console.WriteLine("The character is not alphanumeric.");
            }
        } // End Main()
    }
}

if – nested


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter a character: ");
            char c = (char)Console.Read();
            if (Char.IsLetter(c))
            {
                if (Char.IsLower(c))
                {
                    Console.WriteLine("The character is lowercase.");
                }
                else
                {
                    Console.WriteLine("The character is uppercase.");
                }
            }
            else
            {
                Console.WriteLine("The character isn't an alphabetic character.");
            }
        } // End Main()
    }
}

if – boolean


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // Change the values of these variables to test the results.
            bool Condition1 = true;
            bool Condition2 = true;
            bool Condition3 = true;
            bool Condition4 = true;

            if (Condition1)
            {
                // Condition1 is true.
            }
            else if (Condition2)
            {
                // Condition1 is false and Condition2 is true.
            }
            else if (Condition3)
            {
                if (Condition4)
                {
                    // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
                }
                else
                {
                    // Condition1, Condition2, and Condition4 are false. Condition3 is true.
                }
            }
            else
            {
                // Condition1, Condition2, and Condition3 are false.
            }
        } // End Main()
    }
}

if – AND NOT


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            // NOT
            bool result = true;
            if (!result)
            {
                Console.WriteLine("The condition is true (result is false).");
            }
            else
            {
                Console.WriteLine("The condition is false (result is true).");
            }

            // Short-circuit AND
            int m = 9;
            int n = 7;
            int p = 5;
            if (m >= n && m >= p)
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // AND and NOT
            if (m >= n && !(p > m))
            {
                Console.WriteLine("Nothing is larger than m.");
            }

            // Short-circuit OR
            if (m > n || m > p)
            {
                Console.WriteLine("m isn't the smallest.");
            }

            // NOT and OR
            m = 4;
            if (!(m >= n || m >= p))
            {
                Console.WriteLine("Now m is the smallest.");
            }
            // Output:
            // The condition is false (result is true).
            // Nothing is larger than m.
            // Nothing is larger than m.
            // m isn't the smallest.
            // Now m is the smallest.
        } // End Main()
    }
}

My Website: http://www.lucedigitale.com

References:
http://msdn.microsoft.com/it-it/library/5011f09h.aspx
http://csharp.net-tutorials.com/basics/if-statement/

By |CSharp, Video Games Development|Commenti disabilitati su CSharp – if Statement