Lấy vị trí của các phần tử mảng bằng phương thức BinarySearch.
Đặt một mảng chuỗi -
string[] str = { "a", "m", "i", "t"}; Bây giờ lấy vị trí của ký tự ‘t’ bằng Array.BinarySearch -
Array.BinarySearch(str, "t");
Đây là mã hoàn chỉnh -
Ví dụ
using System;
using System.Text;
public class Demo {
public static void Main() {
string[] str = { "a", "m", "i", "t"};
// Using BinarySearch method to get location of character 't'
int res = Array.BinarySearch(str, "t");
// displaying the location
Console.WriteLine("Index : "+res);
}
} Đầu ra
Index : 3