如何在 C# 中從字串查詢子字串?
設定字串
string s = "Tom Cruise";
現在假設你需要查詢子字串“Tom”,那麼使用 Contains() 方法。
if (s.Contains("Tom") == true) {
Console.WriteLine("Substring found!");
}以下是如何學習從字串中查詢子字串的程式碼 ——
示例
using System;
public class Demo {
public static void Main() {
string s = "Tom Cruise";
if (s.Contains("Tom") == true) {
Console.WriteLine("Substring found!");
} else {
Console.WriteLine("Substring not found!");
}
}
}輸出
Substring found!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP