Sử dụng String Contains trong CSharp | How to use CSharp String Contains?

Phương thức Contains trong lớp String của C# trả về giá trị cho biết một chuỗi chỉ định có tồn tại trong một chuỗi cho trước không? (Contains method in C# String Class returns a value indicating whether a specified substring occurs within this string.)


Syntax - Cú pháp:
bool string.containe(string str)
  • String str - input String for search (chuỗi cần tìm.)


Returns - Giá trị trả về:
  • Boolean - True/False
  • If the str Contains in the String then it returns True.
  • If the str does not Contains in the String it returns False.

Exceptions - Ngoại lệ:
System.ArgumentNullException : If the argument is null (khi đối số truyền vào là null).


Example - Ví dụ:
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            str = "CSharp TOP 10 BOOKS";
            if (str.Contains("TOP") == true)
            {
                MessageBox.Show("The string Contains() 'TOP' ");
            }
            else
            {
                MessageBox.Show("The String does not Contains() 'TOP'");
            }
        }
    }
}

Khi bạn chạy chương trình C# bạn sẽ nhận được thống báo với nội dung là "The string Contains() 'TOP'."
When you run the C# program you will get the MessageBox with message "The string Contains() 'TOP' "




No comments:

Post a Comment