424 次瀏覽
假設您的字串是 −str = "AMIT";要將上述大寫字串轉換為小寫,請使用 ToLower() 方法 −Console.WriteLine("轉換為小寫:{0}", str.ToLower());示例以下是用 C# 轉換字元大小寫的程式碼。線上演示using System; using System.Collections.Generic; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { string str; str = "AMIT"; Console.WriteLine("大寫:{0}", str); // 轉換為小寫 Console.WriteLine("轉換為小寫:{0}", str.ToLower()); Console.ReadLine(); } } }
3K+ 次瀏覽
首先,設定二進位制值 −int num = 101;現在將二進位制賦值給一個新變數 −binVal = num;直到值大於 0,迴圈遍歷二進位制數和基值,如下所示,while (num > 0) { rem = num % 10; decVal = decVal + rem * baseVal; num = num / 10; baseVal = baseVal * 2; }示例以下是將二進位制轉換為十進位制的程式碼。線上演示using System; using System.Collections.Generic; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { ... 閱讀更多
898 次瀏覽
使用 Convert.ToInt32 類來實現將二進位制字串轉換為整數的目的。假設我們的二進位制字串是 −string str = "1001";現在解析每個字元 −try { // 解析傳遞的字串的每個字元 val = Int32.Parse(str1[i].ToString()); if (val == 1) result += (int) Math.Pow(2, str1.Length - 1 - i); else if (val > 1) throw new Exception("無效!"); } catch { throw new Exception("無效!"); }使用 for 迴圈檢查傳遞的字串(即“100”)中的每個字元。查詢… 閱讀更多
首先設定字串 −string str = "Hello World! Hello!";現在檢查字串中“Hello”單詞出現的次數並迴圈遍歷 −while ((a = str1.IndexOf(pattern, a)) != -1) { a += pattern.Length; count++; }示例您可以嘗試執行以下程式碼來統計字串中單詞出現的次數。線上演示using System; class Program { static void Main() { string str = "Hello World! Hello!"; Console.WriteLine("出現次數:" + Check.CheckOccurrences(str, "Hello")); } } public static class Check { public static int CheckOccurrences(string str1, string pattern) { int count ... 閱讀更多
6K+ 次瀏覽
讓我們首先宣告字串 −string str = "Hello World!";現在迴圈遍歷整個字串並查詢空格、製表符或換行符 −while (a
798 次瀏覽
首先,宣告字元陣列並設定每個字元的值 −char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';現在,使用 string 類建構函式並根據上述字元陣列建立一個新字串 −string myChar = new string(ch);示例讓我們看看在 C# 中將字元列表轉換為字串的程式碼。線上演示using System; namespace Demo { class MyApplication { static void Main(string[] args) { char[] ch = new char[5]; ch[0] = ... 閱讀更多
1K+ 次瀏覽
對於阿姆斯特朗數,假設一個數有 3 位數字,則其數字立方和等於該數本身。例如,153 等於 −1³ + 3³ + 5³要使用 C# 檢查它,請檢查該值並找到其餘數。這裡“val”是要檢查阿姆斯特朗數的數字 −for (int i = val; i > 0; i = i / 10) { rem = i % 10; sum = sum + rem*rem*rem; }現在將加法結果與實際值進行比較。如果匹配,則表示… 閱讀更多
820 次瀏覽
建立密碼時,您可能在網站上看到過驗證要求,例如密碼應足夠強,並且具有 −至少 8 個字元,最多 14 個字元至少一個小寫字母無空格至少一個大寫字母至少一個特殊字元讓我們看看如何逐一檢查這些條件 −至少 8 個字元,最多 14 個字元if (passwd.Length < 8 || passwd.Length > 14) return false;至少一個小寫字母if (!passwd.Any(char.IsLower)) return false;無空格if (passwd.Contains(" ")) return false;至少一個大寫字母if (!passwd.Any(char.IsUpper)) return false;檢查是否存在特殊字元string specialCh = @"%!@#$%^&*()?/>.
12K+ 次瀏覽
要檢查字串是否為迴文,您需要首先使用 −Array.reverse() 查詢字串的反轉。之後,使用 equals() 方法將原始字串與反轉的字串進行匹配。如果結果為真,則表示該字串是迴文。示例讓我們嘗試完整的示例。在這裡,我們的字串是“Malayalam”,反轉後結果相同。線上演示using System; namespace palindromecheck { class Program { static void Main(string[] args) { string string1, rev; string1 = "Malayalam"; char[] ... 閱讀更多
10K+ 次瀏覽
要檢查字串是否包含任何特殊字元,您需要使用以下方法 −Char.IsLetterOrDigit在 for 迴圈內使用它並檢查包含特殊字元的字串。假設我們的字串是 −string str = "Amit$#%";現在將字串轉換為字元陣列 −str.ToCharArray();然後,使用 for 迴圈並使用 isLetterOrDigit() 方法檢查每個字元。示例讓我們看看完整的程式碼。線上演示using System; namespace Demo { class myApplication { static void Main(string[] args) { string str = "Amit$#%"; char[] one = str.ToCharArray(); ... 閱讀更多