c#重新string.方法_String.Contains()方法以及C#中的示例

c#重新string.方法

C#String.Contains()方法 (C# String.Contains() Method)

String.Contains() method is used to check whether a given string contains a substring or not, we can use this method when we need to check whether a part of the string (substring) exists in the string.

String.Contains()方法用于检查给定的字符串是否包含子字符串,当需要检查字符串的一部分(子字符串)是否存在时,可以使用此方法。

Syntax:

句法:

    bool String.Contains(String substring);

The method is called with "this" string i.e. the string in which we have to check the substring.

用“ this”字符串调用该方法,即我们必须在其中检查子字符串的字符串。

Parameter:

参数:

  • substring – is the part of the string to be checked.

    substring –是要检查的字符串的一部分。

Return value:

返回值:

  • bool – it returns "True" if substring exists in the string or it returns "False" if substring does not exist in the string.

    布尔 -如果字符串中是否存在子或者子不字符串中存在则返回“假”返回“真”。

Note: This method is case-sensitive.

注意:此方法区分大小写。

Example:

例:

    Input:
    string str  = "Hello world!";
    string str1 = "world";
    string str2 = "Hi";
    
    Function call:
    str.Contains(str1);
    str.Contains(str2);

    Output:
    True
    False

C#使用String.Contains()方法将字符串转换为字符数组的示例 (C# Example to convert string to characters array using String.Contains() method)

Example 1:

范例1:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string str = "Hello world!";
        string str1 = "world";
        string str2 = "Hi";

        // checking substring
        Console.WriteLine("str.Contains(str1): " + str.Contains(str1));
        Console.WriteLine("str.Contains(str2): " + str.Contains(str2));
    }
}

Output

输出量

str.Contains(str1): True
str.Contains(str2): False

Example 2:

范例2:

using System;
class IncludeHelp
{
    static void Main()
    {
        // declaring string variables
        string address = "102, Nehru Place, New Delhi, India.";
        string area1 = "Nehru Place";
        string area2 = "Sant Nagar";

        //checking and printing the result
        if (address.Contains(area1))
        {
            Console.WriteLine(area1 + " exists in the address " + address);
        }
        else
        {
            Console.WriteLine(area1 + " does not exist in the address " + address);
        }

        if (address.Contains(area2))
        {
            Console.WriteLine(area2 + " exists in the address " + address);
        }
        else
        {
            Console.WriteLine(area2 + " does not exist in the address " + address);
        }
    }
}

Output

输出量

Nehru Place exists in the address 102, Nehru Place, New Delhi, India.
Sant Nagar does not exist in the address 102, Nehru Place, New Delhi, India.


翻译自: https://www.includehelp.com/dot-net/string-contains-method-with-example-in-csharp.aspx

c#重新string.方法

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值