반응형
C #에서 문자열을 여러 문자 구분 기호로 나누려면 어떻게합니까?
단어 인 구분자를 사용하여 문자열을 분할하려면 어떻게해야합니까?
예를 들면 다음과 같습니다 This is a sentence
.
나는에 분할 할 is
얻을 This
및 a sentence
.
에서 Java
, 나는 구분 기호로 문자열에 보낼 수 있지만 어떻게이에 달성합니까 C#
?
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
문서의 예 :
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;
// ...
result = source.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Regex.Split 메소드를 다음과 같이 사용할 수 있습니다 .
Regex regex = new Regex(@"\bis\b");
string[] substrings = regex.Split("This is a sentence");
foreach (string match in substrings)
{
Console.WriteLine("'{0}'", match);
}
편집 : 이것은 당신이 준 예제를 만족시킵니다. 일반 String.Split 도 "This"라는 단어의 끝에 " is "로 분할 되므로 Regex 메서드를 사용 하고 " is " 주위에 단어 경계를 포함시킨 이유는 무엇입니까? 그러나이 예제를 실수로 작성했다면 String.Split 이 충분할 것입니다.
이 게시물에 대한 기존 응답을 기반으로 구현을 단순화합니다. :)
namespace System
{
public static class BaseTypesExtensions
{
/// <summary>
/// Just a simple wrapper to simplify the process of splitting a string using another string as a separator
/// </summary>
/// <param name="s"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static string[] Split(this string s, string separator)
{
return s.Split(new string[] { separator }, StringSplitOptions.None);
}
}
}
string s = "This is a sentence.";
string[] res = s.Split(new string[]{ " is " }, StringSplitOptions.None);
for(int i=0; i<res.length; i++)
Console.Write(res[i]);
편집 : "is"는 문장에서 "is"라는 단어 만 제거하고 "this"라는 단어는 그대로 유지 한다는 사실을 유지하기 위해 배열의 공백으로 양쪽에 채워집니다 .
간단히 말해 :
string[] arr = "This is a sentence".Split(new string[] { "is" }, StringSplitOptions.None);
String.Replace ()를 사용하여 원하는 분할 문자열을 문자열에서 발생하지 않는 문자로 바꾼 다음 해당 문자에서 String.Split을 사용하여 결과 문자열을 동일한 효과로 분할 할 수 있습니다.
또는이 코드를 사용하십시오. (동일 : 새 문자열 [])
.Split(new[] { "Test Test" }, StringSplitOptions.None)
다음은 문자열 구분 기호로 분할을 수행하는 확장 함수입니다.
public static string[] Split(this string value, string seperator)
{
return value.Split(new string[] { seperator }, StringSplitOptions.None);
}
사용 예 :
string mystring = "one[split on me]two[split on me]three[split on me]four";
var splitStrings = mystring.Split("[split on me]");
var dict = File.ReadLines("test.txt")
.Where(line => !string.IsNullOrWhitespace(line))
.Select(line => line.Split(new char[] { '=' }, 2, 0))
.ToDictionary(parts => parts[0], parts => parts[1]);
or
enter code here
line="to=xxx@gmail.com=yyy@yahoo.co.in";
string[] tokens = line.Split(new char[] { '=' }, 2, 0);
ans:
tokens[0]=to
token[1]=xxx@gmail.com=yyy@yahoo.co.in
string strData = "This is much easier"
int intDelimiterIndx = strData.IndexOf("is");
int intDelimiterLength = "is".Length;
str1 = strData.Substring(0, intDelimiterIndx);
str2 = strData.Substring(intDelimiterIndx + intDelimiterLength, strData.Length - (intDelimiterIndx + intDelimiterLength));
반응형
'Programing' 카테고리의 다른 글
Visual Studio 2012 Ultimate RC에서 Intellisense 및 코드 제안이 작동하지 않음 (0) | 2020.04.17 |
---|---|
innerText와 innerHTML의 차이점 (0) | 2020.04.17 |
미래와 약속의 차이점은 무엇입니까? (0) | 2020.04.17 |
왜 파이썬 코드를 컴파일해야합니까? (0) | 2020.04.17 |
Java Regex에서 matches ()와 find ()의 차이점 (0) | 2020.04.17 |