不學 JAVA 換學 C# 之覺得心累 - L1:ch11 常見類別的屬性和方法

AdSense

Console 類別

  • WriteLine(string value):在控制台上輸出資料,並提供換行。
  • ReadLine()::從控制台讀取一行資料。

範例:

1
2
3
Console.WriteLine("請輸入您的名字:");
string name = Console.ReadLine();
Console.WriteLine($"你好, {name}!");

String 類別

  • Length:獲得字串的長度。
  • Substring(int startIndex, int length):擷取字串中的一部分。
  • Replace(string oldValue, string newValue):將某些字符替換為其他值。
  • Contains(string value):檢查字串是否包含特定子字串。
  • Concat(params string[] values):將多個字串連接在一起。
  • Format(string format, params object[] args):格式化字串。
  • Split(params char[] separator):根據分隔符將字串分割成陣列。
  • Join(string separator, params string[] values):將陣列中的元素,加入分隔符連接成一個字串。
  • IsNullOrEmpty(string value):檢查字串是否為 null 或空字串。
  • IsNullOrWhiteSpace(string value):檢查字串是否為 null、空字串或只包含空白字符。

範例:

1
2
3
4
5
6
7
8
9
10
string text = "Hello, World!";
string subText = text.Substring(7, 5); // "World"
string replacedText = text.Replace("World", "C#"); // "Hello, C#!"
bool containsHello = text.Contains("Hello"); // true
string concatenated = string.Concat("Hello!", " I'm Jenifer."); // "Hello! I'm Jenifer."
string formatted = string.Format("{0} {1}", "Hello", "World"); // "Hello World"
string[] words = text.Split(','); // { "Hello", " World!" }
string joined = string.Join("-", words); // "Hello- World!"
bool isNullOrEmpty = string.IsNullOrEmpty(text); // false
bool isNullOrWhiteSpace = string.IsNullOrWhiteSpace(" "); // true

Math 類別

  • Abs(double value):求絕對值。
  • Pow(double x, double y):計算次方值。
  • Sqrt(double value):求平方根。

範例:

1
2
3
4
double number = -9.5;
double absValue = Math.Abs(number); // 9.5
double powValue = Math.Pow(2, 3); // 8
double squareRootValue = Math.Sqrt(16); // 4.0

Array 類別

T 可以是任何型態或類別,例如:int[] arrayPerson[] array

  • Length:計算陣列元素的個數。
  • Sort(T[] array):將陣列進行排序 (由小到大)。
  • Reverse(T[] array):反轉陣列順序。
  • IndexOf(T[] array, T value):回傳指定元素在陣列中的索引值,如果找不到則回傳 -1。
  • LastIndexOf(T[] array, T value):回傳最後一個出現的指定元素的索引。
  • Clear(T[] array, int index, int length):將陣列中指定範圍的元素設為預設值。數值型會變成 0,參考類型會變成 null
  • BinarySearch(T[] array, T value):對已排序的陣列進行二分搜尋。BinarySearch 使用之前陣列一定要排序好,否則結果不正確。回傳在陣列中的索引位置,或回傳一個負數

    回傳值的定義:

    The index of the specified value in the specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.

    如果 value 存在於陣列中,則回傳 value 在陣列中的索引位置;否則,回傳一個負數。如果 value 不存在,且 value 小於陣列中的一個或多個元素,則回傳的是 bitwise complement 的值,這個值是第一個比 value 大的元素的索引。如果 value 不存在,且 value 大於陣列中所有元素,則回傳的是最後一個元素索引 +1 的 bitwise complement。

範例:

1
2
3
4
5
6
7
8
9
10
11
int[] numbers = { 3, 1, 4, 1, 5 };
Array.Sort(numbers); // { 1, 1, 3, 4, 5 }
Array.Reverse(numbers); // { 5, 4, 3, 1, 1 }
int index = Array.IndexOf(numbers, 4); // 1 (因為 numbers 已經被排序又反轉過了)
int lastIndex = Array.LastIndexOf(numbers, 1); // 4
Array.Clear(numbers, 1, 2); // { 5, 0, 0, 1, 1 }

int[] numbers2 = { 1, 3, 5, 7, 9 };
int index = Array.BinarySearch(numbers2, 7); // 3
int notFound = Array.BinarySearch(numbers2, 6); // -4
// 因為找不到,會回傳:~第一個比 6 大的元素的索引 = ~3 = ~0011 = 1100 = -4

參考資料:
Array.BinarySearch Method
Bitwise and shift operators (C# reference)