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.方法