Array.IndexOf Method

Array.IndexOf Method

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a string array with 3 elements having the same value.
            String[] strings = { "the", "quick", "brown", "fox", "jumps",
                            "over", "the", "lazy", "dog", "in", "the",
                            "barn" };

            // Display the elements of the array.
            Console.WriteLine("The array contains the following values:");
            for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
                Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

            // Search for the first occurrence of the duplicated value.
            String searchString = "the";
            int index = Array.IndexOf(strings, searchString);
            Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                              searchString, index);

            // Search for the first occurrence of the duplicated value in the last section of the array.
            index = Array.IndexOf(strings, searchString, 4);
            Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                              searchString, index);

            // Search for the first occurrence of the duplicated value in a section of the array.
            int position = index + 1;
            index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
            Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                              searchString, position, strings.GetUpperBound(0), index);
        }
    }
}

ผลการรัน

The array contains the following values:
   [ 0]: the
   [ 1]: quick
   [ 2]: brown
   [ 3]: fox
   [ 4]: jumps
   [ 5]: over
   [ 6]: the
   [ 7]: lazy
   [ 8]: dog
   [ 9]: in
   [10]: the
   [11]: barn
The first occurrence of "the" is at index 0.
The first occurrence of "the" between index 4 and the end is at index 6.
The first occurrence of "the" between index 7 and index 11 is at index 10.

แต่ถ้าค้นหาไม่เจอจะคืนค่าเป็น -1

NodaTime

  1. การติดตั้ง
  2. การหาจำนวน วัน เดือน ปี ระหว่าง 2 ช่วงเวลาด้วย NodaTime
  3. การใช้ NodaTime ร่วมกับ DateTime
Continue reading

DateTime

  1. แสดงปีเป็น พ.ศ. และ ค.ศ. ด้วย CultureInfo()
  2. การแปลง string เป็น DateTime โดยใช้ ParseExact()
  3. การเพิ่ม-ลด วัน เดือน ปี
  4. การหาจำนวนวันระหว่าง 2 DateTime
  5. การ for loop วัน – เวลา
  6. แสดงวันที่จาก DateTime แบบต่างๆ
  7. ตรวจสอบวันที่ ถ้า T1 น้อยกว่า T2 ให้ Error
  8. ตรวจสอบว่า DateTime? เป็น null หรือไม่
  9. DateTime.TryParseExact()
Continue reading

เก็บข้อมูลลง Session, ViewBag

ให้รันเรียกหน้า About ก่อน แล้วค่อยเรียกหน้า Contact

  • myVar1 ใช้ได้ที่ About.cshtml
  • Session[“myVar2”] ใช้ได้ทุกหน้า
  • ViewBag.myVar3 ใช้ได้ที่ About.cshtml และ HomeController.cs
  • Session[“myVar4”] ใช้ได้ทุกหน้า
  • ViewBag.myVar5 ใช้ได้ที่ About.cshtml และ HomeController.cs
Continue reading

สร้าง Form ส่งข้อมูลด้วย JavaScript

<!DOCTYPE html>
<html>

<head>
    <script>
        function redirectPost(url, data) {
            var form = document.createElement('form');
            document.body.appendChild(form);
            form.method = 'post';
            form.action = url;
            for (var name in data) {
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = name;
                input.value = data[name];
                form.appendChild(input);
            }
            form.submit();
        }
    </script>
</head>

<body>
    <p>Press button to POST and redirect</p>
    <button type="button" onclick="redirectPost('http://www.example.com', { username: 'jack', status: 'ok' });">Try
        it</button>
</body>

</html>

Form submit() Method

Form submit() Method

<!DOCTYPE html>
<html>
<body>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
function myFunction() {
  document.getElementById("myForm").submit();
}
</script>

</body>
</html>

onload Event

<!DOCTYPE html>
<html>
<body onload="myFunction()">
<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
</form>
<script>
function myFunction() {
  document.getElementById("myForm").submit();
}
</script>

</body>
</html>