Skip to Navigation Skip to Content

Posts Tagged ‘ พื้นฐาน ’

Unit 4 String

bot2.gifString >> เป็นสายของอักขระ หรือเป็นการนำเอา character หลายตัวมาต่อกันในลักษณะของอาร์เรย์ โดยมี Method ที่ำสำคัญ

String Method more

Length Compare Concat
CopyTo IndexOf
Replace Substring
ToString ToUpper
  • Length >> เป็นการหาความยาวของสตริงที่กำหนด more top
    // Sample for String.Length

    using System;

    class Sample

    {

    public static void Main()

    {

    string str = “abcdefg”;

    Console.WriteLine(”1) The length of ‘{0}’ is {1}”, str, str.Length);

    Console.WriteLine(”2) The length of ‘{0}’ is {1}”, “xyz”, “xyz”.Length);

    }

    }

    /*This example produces the following results:

    1) The length of ‘abcdefg’ is 7

    2) The length of ‘xyz’ is 3

    */

  • Compare >> ใช้ในการเปรียบเทียบสตริง 2 ตัว โดยหากตัวแรกยาวกว่าตัวที่สองจะคืนค่า 1 หากสั้นกว่าจะคื่นค่า -1 แต่ถ้าหากเท่ากันจะคืนค่า 0 more top
    using System;

    class Program

    {

    static void Main(string[] args)

    {

    string str1 = “1″;

    string str2 = “12″;

    string str3 = “123″;

    int i = String.Compare(str1,str1);

    int j = String.Compare(str1, str3);

    int k = String.Compare(str3, str2);

    Console.WriteLine(”{0}”,i); // str1 = str1 so result = 0

    Console.WriteLine(”{0}”, j); // str1 < str3 so result = -1

    Console.WriteLine(”{0}”, k); // str3 > str2 so result = 1

    Console.Read();

    }

    }

  • Concat >> เป็นการนำสตริงมาต่อกัน more top
    using System;

    class ConcatTest

    {

    static void Main(string[] args)

    {

    string str1 = “Hello”;

    string str2 = “World”;

    string str3 = String.Concat(str1,str2);

    Console.WriteLine(str3);

    Console.Read();

    }

    }

    // Result is HelloWorld

  • Copy >> เป็นการคัดลอกสตริง more top
    // Sample for String.Copy()

    using System;

    class Sample

    {

    public static void Main()

    {

    string str1 = “abc”;

    string str2 = “xyz”;

    str2 = String.Copy(str1);

    Console.WriteLine(”str1 = ‘{0}’”, str1);

    Console.WriteLine(”str2 = ‘{0}’”, str2);

    }

    }

    /*This example produces the following results:

    str1 = ‘abc’

    str2 = ‘abc’*/

  • CopyTo >> เป็นการคัดลอกอักขระตามจำนวน และตำแหน่งเริ่มต้นที่ระบุ ไปเก็บไว้ในรูปแบบของอาร์เรย์ more
    top

    using System;

    public class CopyToTest

    {

    public static void Main()

    {

    // Embed an array of characters in a string

    string strSource = “changed”;

    char[] destination = { ‘T’, ‘h’, ‘e’, ‘ ‘, ‘i’, ‘n’, ‘i’, ‘t’, ‘i’, ‘a’, ‘l’, ‘ ‘,

    ‘a’, ‘r’, ‘r’, ‘a’, ‘y’ };

    // Print the char array

    Console.WriteLine(destination);

    // Embed the source string in the destination string

    strSource.CopyTo(0, destination, 4, strSource.Length);

    // Print the resulting array

    Console.WriteLine(destination);

    Console.Read();

    }

    }

  • Equals >> เปรียบเทียบว่าสตริง 2 ตัวเหมือนกันหรือไม่ หากเหมือนจะคืนค่า true more
    top

    using System;

    using System.Text;

    class Sample

    {

    public static void Main()

    {

    string str1 = “aaa”;

    string str2 = “bbb”;

    string str3 = “bbb”;

    bool x = String.Equals(str1,str2);

    bool y = String.Equals(str2, str3);

    Console.WriteLine(x); // str1 is not equal str2,so result is Fault

    Console.WriteLine(y); // str2 is equal str3,so result is True

    Console.Read();

    }

    }

  • IndexOf >> เป็นการตรวจสอบตำแหน่งของอักขระในสตริงที่กำหนด หากมีสตริงซ้ำกันหลายตัวจะพิจารณาตัวแรกที่เจอ โดยตำแหน่งเริ่มต้นลำดับเป็น 0 more
    top

    using System;

    public class IndexOfTest

    {

    public static void Main()

    {

    string str1 = “Today is Monday”;

    int i = str1.IndexOf(”is”);

    Console.WriteLine(i);// result is 6

    Console.Read();

    }

    }

  • Insert >> เป็นการแทรกสตริงเข้าไปในตำแหน่งที่กำหนด more
    top

    using System;

    public class InsertTest

    {

    public static void Main()

    {

    string str1 = “Today is Monday”;

    str1 = str1.Insert(9,”not “);

    Console.WriteLine(str1);

    Console.Read();

    }

    }

  • Remove >>เป็นการลบสตริงจากตำแหน่งและจำนวนที่กำหนด more
    top

    using System;

    public class RemoveTest

    {

    public static void Main()

    {

    string str1 = “Today is not Monday”;

    string str2 = str1.Remove(9,3);

    Console.WriteLine(str2); //Result : Today is Monday

    Console.Read();

    }

    }

  • Replace>> เป็นการแทนที่สตริงเดิมด้วยสตริงใหม่ more
    top

    using System;

    class stringReplace1

    {

    public static void Main()

    {

    String str = “1 2 3 4 5 6 7 8 9″;

    Console.WriteLine(”Original string: \”{0}\”", str);

    Console.WriteLine(”CSV string: \”{0}\”", str.Replace(’ ‘, ‘,’));

    }

    }

    // This example produces the following output:

    // Original string: “1 2 3 4 5 6 7 8 9″

    // CSV string: “1,2,3,4,5,6,7,8,9″

  • Split >> เป็นการคัดแยกสตริงด้วย character ที่กำหนด ผลลัพธ์ที่ได้เก็บไว้ในอาร์เรย์สตริง more
    top

    using System;

    public class SplitTest

    {

    public static void Main()

    {

    string words = “this is a list of words, with: a bit of punctuation.”;

    string[] split = words.Split(new Char[] { ‘ ‘, ‘,’, ‘.’, ‘:’ });

    foreach (string s in split)

    {

    if (s.Trim() != “”)

    Console.WriteLine(s);

    }

    Console.Read();

    }

    }

  • Substring >> เป็นการหาสตริงย่อยภายในสตริงที่กำหนด more
    top

    using System;

    public class SubStringTest

    {

    public static void Main()

    {

    string str1 = “Microsoft Visual C# “;

    string str2 = str1.Substring(0,9);

    Console.WriteLine(str2); //Result : Microsoft

    Console.Read();

    }

    }

  • ToLower >> แปลงสตริงทั้งหมดเป็นตัวพิมพ์เล็ก more
    top

    using System;

    public class ToLowerTest

    {

    public static void Main()

    {

    string str1 = “FRIENDS FOREVER”;

    str1 = str1.ToLower();

    Console.WriteLine(str1); //Result : friends forever

    Console.Read();

    }

    }

  • ToString >> แปลงค่าที่กำหนดให้เป็นสตริง more
    top

    using System;

    class stringToString

    {

    public static void Main()

    {

    String str1 = “123″;

    String str2 = “abc”;

    Console.WriteLine(”Original str1: {0}”, str1);

    Console.WriteLine(”Original str2: {0}”, str2);

    Console.WriteLine(”str1 same as str2?: {0}”, Object.ReferenceEquals(str1, str2));

    str2 = str1.ToString();

    Console.WriteLine();

    Console.WriteLine(”New str2:{0}”, str2);

    Console.WriteLine(”str1 same as str2?: {0}”, Object.ReferenceEquals(str1, str2));

    Console.Read();

    }

    }

    /*This code produces the following output:

    Original str1: 123

    Original str2: abc

    str1 same as str2?: False

    New str2:123

    str1 same as str2?: True*/

  • ToUpper >> แปลงสตริงทั้งหมดเป็นตัวพิมพ์ใหญ่ more
    top

    using System;

    public class ToUpperTest

    {

    public static void Main()

    {

    string str1 = ” friends forever”;

    str1 = str1.ToLower();

    Console.WriteLine(str1); //Result : FRIENDS FOREVER

    Console.Read();

    }

    }

  • TrimEnd >> ตัดช่องว่างที่อยู่ท้ายสตริงนั้นออก more
    top

    using System;

    public class TrimTest

    {

    public static void Main()

    {

    string str1 = “FRIENDS FOREVER “;

    str1 = str1.TrimEnd();

    Console.WriteLine(str1); //Result : FRIENDS FOREVER

    Console.Read();

    }

    }

เรื่องที่เกี่ยวข้อง

Page 1 of 3123»

Unit 3 Array

bot2.gifArray

  • รูปแบบในการสร้างอาร์เรย์ >> มีดังนี้

    รูปแบบของคำสั่ง

    ตัวอย่าง

    DataType [] ArrayName = new DataType[Dimention]; int[] MyArray = new int[10];

    Dimention : มิติของอาร์เรย์ หรือจำนวนสมาชิก โดยที่สมาชิกตัวแรกจะมีลำดับเป็น 0 เสมอ

  • การกำหนดค่าให้อาร์เรย์ >> มี 2 วิธี1. กำหนดโดยการแจกแจงสมาชิกแต่ละตัว เช่น
    MyArray[0] = 0;

    MyArray[1] = 10;

    MyArray[2] = 20;

    …………………..

    2. กำหนดในลักษณะของเซต

    รูปแบบของคำสั่ง

    ตัวอย่าง

    DataType [] ArrayName = {element 1,element 2,….element n} int[] Arr = {100,200,300};
  • การใช้ข้อมูลในอาร์เรย์

    รูปแบบของคำสั่ง

    ตัวอย่าง

    foreach (dataType variable in ArrayName){

    statement;

    }

    foreach(int i in MyArray){

    Console.WriteLine(i);

    }

  • การเรียงลำดับข้อมูลในอาร์เรย์

    รูปแบบของคำสั่ง

    ตัวอย่าง

    Array.Sort(ArrayName,Startndex,Length); Array.Sort(MyArr,3,5);

    StartIndex : ลำดับเริ่มต้นในการเรียงลำดับ หากไม่ระบุหมายถึงเรียงทุกตัว

    Length : จำนวนสมาชิกที่ต้องการเรียงลำดับนับจาก
    StartIndex

    NOTE : ถ้าเป็นตัวเลขจะเรียงจากน้อยไปมาก แตุ่้ถ้าเป็นตัวอักษรจะเรียงจาก A-Z

  • การเรียงลำดับแบบย้อนกลับ

    รูปแบบของคำสั่ง

    ตัวอย่าง

    Array.Reverse(ArrayName,Startndex,Length); Array.Reverse(MyArr,3,5);
  • การคัดลอกอาร์เรย์

    รูปแบบของคำสั่ง

    ตัวอย่าง

    Array.Copy(SourceArray,SourceIndex,DesArray,DesIndex,Length); Array.Copy(Arr1,5,Arr2,0,5);

    SourceArray : อารเรยที่ี่เราต้องการคัดลอกข้อมูล

    SourceIndex : ตำแหน่งที่จะเริ่มทำการคัดลอกข้อมูล

    DesArray : อาร์เรย์ที่เราต้องการนำข้อมูลใส่ลงไป

    DesIndex : ตำแหน่งเริ่มแรกที่เราต้องการใส่ข้อมูลลงไป

    Length : ความยาว หรือจำนวนสมาชิกที่ต้องการก๊อปปี้ไป

  • การลบค่าของอาร์เรย์

    รูปแบบของคำสั่ง

    ตัวอย่าง

    Array.Clear(ArrayName,Startndex,Length); Array.Clear(MyArr,3,5);
  • การตรวจสอบลำดับในอาร์เรย์

    รูปแบบของคำสั่ง

    ตัวอย่าง

    Array.IndexOf(ArrayName,Value); Array.IndexOf(MyArr,”ddd”);

    Value : สมาชิกที่ต้องการทราบอันดับ

  • การหาความยาว

    รูปแบบของคำสั่ง

    ตัวอย่าง

    ชื่อของอาร์เรย์.Length int[] intArr = {1,2,3,4,5};

    int len = intArr.Length; //len = 5

  • การหาขอบเขตของอาร์เรย์1.ขอบเขตบน หรือสมาชิกตัวแรก (Upper Bound)

    รูปแบบของคำสั่ง

    ตัวอย่าง

    ชื่อของอาร์เรย์.GetUpperBound(Dimension) int[] intArr = {1,2,3,4,5};

    int upper = intArr.GetUpperBound(0); // upper= 4

    2. ขอบเขตล่าง หรือสมาชิกตัวสุดท้าย (Lower Bound)

    รูปแบบของคำสั่ง

    ตัวอย่าง

    ชื่อของอาร์เรย์.GetLowerBound(Dimension) int[] intArr = {1,2,3,4,5};

    int lower = intArr.GetLowerBound(0); // lower = 0

  • อาร์เรย์หลายมิติ >> อาร์เรย์ที่สมาชิกแต่ละตัวเก็บข้อมูลมากกว่า 1 ค่า ส่วนใหญ่จะใช้ไม่เกิน 2 มิติ

    รูปแบบของคำสั่ง

    ตัวอย่าง

    DataType [,] ArrayName = new DataType[UpperB,LowerB]; int[] MyArray = new int[5,2];
  • การกำหนดข้อมูลให้กับอาร์เรย์หลายมิติ1. กำหนดโดยการแจกแจงสมาชิกแต่ละตัว เช่น
    MyArr[0,0] = 0;

    MyArr[0,1] = 1;

    MyArr[1,0] = 2;

    MyArr[1,1] = 3;

    2. กำหนดในลักษณะของเซต

    int[,] MyArr ={{1,2} , {3,4} ,{5,6}};

    string[,] animal = {{”ant,bird”},{”cat,dog”}};

เรื่องที่เกี่ยวข้อง

Page 1 of 3123»

Unit 2 : การกำหนดเงื่อนไข

bot2.gifรูปแบบของการกำหนดเงื่อนไข

  • Selection Statement เป็นการเลือกทำเมื่อตรงกับเงื่อนไขที่ต้องการ
  • Iteration Statement เป็นการวนรอบเพื่อตามคำสั่งเดียวกันซ้ำๆ ภายใต้เงื่อนไขที่กำหนด
  • Jump Statement เป็นการกระโดดไปยังส่วนอื่นของโปรแกรม เมื่อโปรแกรมประมวลผลมาถึงตำแหน่งนั้นๆ

bot2.gifSelection Statement

  • If >> เป็นการเปรียบเทียบเงื่อนไขว่าตรงตามที่เรากำหนดหรือไม่ หากตรงก็จะทำตามคำสั่งที่กำหนด แต่หากไม่ตรงก็จะข้ามคำสั่งส่วนนั้นไป

    รูปแบบของคำสั่ง

    ตัวอย่าง

    if( condition){

    statement;

    }

    if(x==y){

    Console.Write(”x equals y”);

    }

  • If…else >> เป็นการกำหนดทางเลือกอื่นๆในการทำงานนอกเหนือจากที่กำหนดใน if โดยที่หากเงื่อนไขไม่ตรงกับที่ระบุไว้ใน if โปรแกรมจะทำตามคำสั่งที่ระบุในส่วนของ else

    รูปแบบของคำสั่ง

    ตัวอย่าง

    if( condition){

    statement; // do if condition match

    }

    else{

    statement; // do other things

    }

    if(x>y){

    z = x - y;

    }

    else{

    z = y - x;

    }

  • else if >> ใช้ในกรณีที่เงื่อนไขที่เราต้องการตรวจสอบมีหลายเงื่อนไข

    รูปแบบของคำสั่ง

    ตัวอย่าง

    if( condition){

    statement;

    }

    else if( condition){

    statement;

    }

    else{

    statement;

    }

    if( score >= 90){

    grade = “A” ;

    }

    else if( score >= 80){

    grade = “B” ;

    }

    else{

    grade = “D”;

    }

  • Nest if >> ใช้เมื่อต้องการตรวจสอบเงื่อนไขหลายๆเงื่อนไขร่วมกัน

    รูปแบบของคำสั่ง

    ตัวอย่าง

    if(condition){

    if(condition){

    if(condition){

    statement;

    }

    else{

    statement;

    }

    }

    }

    if(x>5){

    if(y>30){

    if(x != y){

    x += y ;

    }

    else{

    x -= y;

    }

    }

    }

  • switch…case >> ใช้ในกรณีที่เราีมีหลายเงื่อนไขที่ต้องตรวจสอบ วิธีนี้จะสะดวกกว่าการใช้ else if หลายๆรอบ

    รูปแบบของคำสั่ง

    ตัวอย่าง

    switch(expression){

    case value 1: statement 1;

    break;

    case value 2: statement 2;

    break;

    case value 3: statement 3;

    break;

    case value n: statement n;

    break;

    default : statement; // ใช้ในกรณีที่ไม่เข้ากับเงื่อนไขใดเลย

    }

    d = int.Parse(Console.ReadLine());

    switch(d){

    case value 1: myday = “Sunday”;

    break;

    case value 2: myday = “Monday”;

    break;

    case value 3: myday = “Tueday”;

    break;

    case value 4: myday = “Wednesday”;

    break;

    default : myday = “Unknown”;

    }

bot2.gifIteration Statement

  • for >> เป็นการทำงานซ้ำตามจำนวนครั้งที่กำหนด โดยเริ่มจากค่าแรกไปจนถึงค่าสุดท้าย

    รูปแบบของคำสั่ง

    ตัวอย่าง

    for(counter = first value; counter condition; adjust counter value){

    statement;

    }

    for(int i=1;i<=10;i++){

    sum +=i;

    }

  • Nest for…loop >> เป็นการวบลูปซ้อนกัน เมื่อมีหลายเงื่อนไข

    รูปแบบของคำสั่ง

    ตัวอย่าง

    for(){

    for(){

    for(){

    statement;

    }

    }

    }

    for(i=0;i<5;i++){

    for(j=0;j>9;j++){

    for(k=0;k>=17;k++){

    sum += i+j+k;

    }

    }

    }

  • foreach…in >>ใช้ในกรณีที่ตัวแปรตัวนั้นเก็บข้อมูลหลายค่าเอาไว้ เช่น Array

    รูปแบบของคำสั่ง

    ตัวอย่าง

    foreach( ชนิดข้อมูล ตัวแปร in ตัวแปรที่เก็บข้อมูล){

    statement;

    }

    foreach(string s in args){

    Console.WriteLine(s);

    }

  • while >> ใช้เมื่อทราบเงื่อนไขบางอย่าง แต่ไม่ทราบค่าเริ่มต้นและค่าสุดท้ายของลูป

    รูปแบบของคำสั่ง

    ตัวอย่าง

    while(condition){

    statement;

    }

    while(x<100){

    x += 1 ;

    }

  • do…while…>> เป็นการตรวจสอบเงื่อนไขคล้าย while แต่do..while จะตรวจสอบเงื่อนไขที่ท้ายลูป

    รูปแบบของคำสั่ง

    ตัวอย่าง

    do{

    statement;

    }while(condition);

    do{

    sum +=1

    }while(sum<10);

bot2.gifJump Statement

  • break >> เป็นการสั่งให้ออกจากลูปโดยการหยุดการตรวจสอบเงื่อนไขถัดไป
  • continue >> เป็นการสั่งให้วนลูปถัดไปโดยไม่ทำคำสั่งที่เหลือในลูปนั้น

เรื่องที่เกี่ยวข้อง

Page 1 of 3123»

Theme by Dezinerfolio | Edit by : NongOff