누군가가 나를 도울 수 있기를 바랍니다. 나는 C#을 배웠고 간단한 질문이 있습니다.문자열이 다른 문자열에 있는지 확인하는 방법
변수가 있는데 다른 문자열에 있는지 확인하고 싶습니다.
if (test contains "abc") {
}
같은 뭔가 일을 할 것입니다 C#으로
누군가가 나를 도울 수 있기를 바랍니다. 나는 C#을 배웠고 간단한 질문이 있습니다.문자열이 다른 문자열에 있는지 확인하는 방법
변수가 있는데 다른 문자열에 있는지 확인하고 싶습니다.
if (test contains "abc") {
}
같은 뭔가 일을 할 것입니다 C#으로
사용하여 존재하지 않는 경우 ... 거기에 String.Contains :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
대소 문자를 구별하지 않기 위해 아래의 답을 참조하십시오. – Pellet
IndexOf()
기능을이 작업을 수행 할 수있는 쉬운 방법은
그것은 -1을 반환합니다 문자열이
string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);
이 코드는 문자열 내에서 하위 문자열을 검색하는 방법을 보여 주며 시작 위치의 인덱스 위치를 반환하거나 문자열이 발견되지 않았 음을 나타내는 -1을 반환합니다.
Contains()
을 사용할 수도 있습니다. Contains는 문자열 유형에 대한 인스턴스 메서드로, 프로그램의 특정 문자열에서 호출 할 수 있습니다. bool 결과를 가지며, 매개 변수가 발견되면 true이고, 매개 변수가 없으면 false입니다.
using System;
class Program
{
static void Main()
{
Test("Dot Net Perls");
Test("dot net perls");
}
static void Test(string input)
{
Console.Write("--- ");
Console.Write(input);
Console.WriteLine(" ---");
//
// See if the string contains 'Net'
//
bool contains = input.Contains("Net");
//
// Write the result
//
Console.Write("Contains 'Net': ");
Console.WriteLine(contains);
//
// See if the string contains 'perls' lowercase
//
if (input.Contains("perls"))
{
Console.WriteLine("Contains 'perls'");
}
//
// See if the string contains 'Dot'
//
if (!input.Contains("Dot"))
{
Console.WriteLine("Doesn't Contain 'Dot'");
}
}
}
문자열에 대해서는 아무것도 확인하지 않습니다.
This을 참조하십시오.
String.Contains(...)
정규 표현식을 사용해야합니다. 예 : Regex.IsMatch(test, "abc")
. 테스트에 abc가 포함되어 있으면 true를 반환합니다.
위에서 볼 수 있듯이 정규 표현식을 사용하지 않아도됩니다.실제로, 그 방법은 그들 모두의 방법을 이해하는 데 가장 둔하고, 가장 느리고, 가장 힘들 수 있습니다. – VoidKing
String.Contains(...)
을 사용하면 좋지 않을 수 있습니다.
String.Contains(...)
대소 문자 구분 비교를 수행합니다. 따라서 대소 문자 일치에주의하십시오. 이에 대한 String.Contains
을 사용할 수 있습니다
사용을 확인하기 전에
당연히 당신은 ToLower()
또는 ToUpper()
를 사용할 수 있습니다.
if (test.Contains("abc"))
{
// Your Code Here
}
if (stringValue.ToUpper().Contains("FIND_THIS"))
{
// Do Something //
}
는 대소 문자를 구분 검색을위한 또 다른 좋은 변화입니다.
는 CompareTo
, Contains
, Compare
, IndexOfAny
및 IndexOf
처럼이 유효성 검사를 할 수있는 몇 가지 방법이 있습니다.
String
클래스의 메소드 목록을 확인하십시오.
string s1 = "ani\u00ADmal";
string s2 = "animal";
string s3 = "abc";
string s4 = "abc";
string s5 = "ABC";
bool b1 = s1.CompareTo(s2) > -1; // return true, exists
bool b2 = s3.CompareTo(s4) > -1; // return true, exists
bool b3 = s3.CompareTo(s5) > -1; // return false, no case sensitive, no exists
bool b4 = s1.Contains(s2); // return false, no exists
bool b5 = s3.Contains(s4); // return true, exists
bool b6 = s3.Contains(s5); // return false, no case sensitive, no exists
string s6 = "MACHINE";
string s7 = "machine";
string s8 = "nature";
int a = String.Compare(s6, 0, s7, 0, s6.Length, true); // return 0, contain and is less
int b = String.Compare(s6, 0, s7, 0, s6.Length, false); // return 1, contain and is greater
int c = String.Compare(s6, 0, s8, 0, s6.Length, true); // return -1, no contain
int d = String.Compare(s6, 0, s8, 0, s6.Length, false); // return -1, no contain
int e = s1.IndexOfAny(s2.ToCharArray()); // return 0, exists
int f = s3.IndexOfAny(s4.ToCharArray()); // return 0, exists
int g = s3.IndexOfAny(s5.ToCharArray()); // return -1, no case sensitive, no exists
int h = s1.IndexOf(s2); // return 0, exists
int i = s3.IndexOf(s4); // return 0, exists
int j = s3.IndexOf(s5); // return -1, no exists
는 http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx – Marlon
Geepers을 ... 당신은 거의 대답 질문 한 그. API 문서 루크를 사용하십시오. 힘은 sooo입니다. 90 's – corlettk