성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>리눅스 환경의 .NET Core에서 "test".IndexOf("\0")가 0을 반환</h1> <p> 희한하군요. 다음의 코드가 윈도우와 리눅스에서 결과가 다릅니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > string txt = "test"; Console.WriteLine(txt.IndexOf("\0")); // 윈도우 환경: -1 반환 // 리눅스 환경: 0 반환 </pre> <br /> 심지어 다음과 같이 해도 마찬가지입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > string txt = "test\0\0test"; Console.WriteLine(txt.IndexOf("\0")); // 윈도우에서는 4 반환 // 리눅스에서는 0 반환 </pre> <br /> <hr style='width: 50%' /><br /> <br /> 검색해 보면 다음의 글이 나오는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > What happens when you IndexOf an empty string? ; <a target='tab' href='https://dev.to/turnerj/what-happens-when-you-indexof-an-empty-string-1m0c'>https://dev.to/turnerj/what-happens-when-you-indexof-an-empty-string-1m0c</a> </pre> <br /> 빈 문자열의 경우에는 윈도우와 리눅스 환경에서 동일하게 0을 반환합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > string txt = "test"; Console.WriteLine(txt.IndexOf("")); // 윈도우 및 리눅스 환경: 0 반환 </pre> <br /> null 문자와는 다르게, 사실 빈 문자열에 대해서는 어떤 값이 반환되어야 할 지 판단 기준이 잘 안 서는데요, 이에 대해 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.8#System_String_IndexOf_System_String'>MSDN 문서</a>에 설명이 있습니다.<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> <span style='color: blue; font-weight: bold'>Character sets include ignorable characters</span>, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if value contains an ignorable character, the result is equivalent to searching with that character removed. <span style='color: blue; font-weight: bold'>If value consists only of one or more ignorable characters, the IndexOf(String, Int32, Int32) method always returns startIndex</span>, which is the character position at which the search begins. </div><br /> <br /> 위의 이야기에 따르면 "ignorable characters"에 포함하는 문자를 IndexOf로 찾는 경우에는 startIndex의 값을 반환한다고 합니다. 실제로 테스트 결과 그렇게 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.WriteLine(txt.IndexOf("\0")); // 0 반환 Console.WriteLine(txt.IndexOf("\0", 2)); // 2 반환 </pre> <br /> 정리해 보면, 빈 문자열과 '\0' 널 문자는 리눅스 환경에서는 "ignorable characters"에 속하고, 윈도우 환경에서는 빈 문자열만 해당하는 것입니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 참고로 "<a target='tab' href='https://dev.to/turnerj/what-happens-when-you-indexof-an-empty-string-1m0c'>What happens when you IndexOf an empty string?</a>" 글에서 인용한 내용을 보면 힌트가 하나 더 있습니다.<br /> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. <span style='color: blue; font-weight: bold'>In a culture-sensitive search (that is, if comparisonType is not Ordinal or OrdinalIgnoreCase)</span>, if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, the IndexOf(String) method always returns 0 (zero) to indicate that the match is found at the beginning of the current instance. </div><br /> <br /> 따라서 리눅스에서도 StringComparison의 옵션에 따라 결과가 달라집니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.WriteLine(txt.IndexOf("\0", StringComparison.Ordinal)); // -1 반환 Console.WriteLine(txt.IndexOf("\0", StringComparison.OrdinalIgnoreCase)); // -1 반환 Console.WriteLine(txt.IndexOf("\0", StringComparison.InvariantCulture)); // 0 반환 Console.WriteLine(txt.IndexOf("\0", StringComparison.CurrentCultureIgnoreCase)); // 0 반환 Console.WriteLine(txt.IndexOf("\0", StringComparison.InvariantCultureIgnoreCase)); // 0 반환 </pre> <br /> 문서에서 언급한 것처럼 "Ordinal or OrdinalIgnoreCase" 상황에서는 저렇게 -1을 정상적으로 반환합니다.<br /> <br /> 마지막으로 다음의 결과도 인상적입니다.<br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string result = null; result = "\r\n"; // Environment.NewLine == A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms. int pos = result.IndexOf(Environment.NewLine); Console.WriteLine(pos); // -1 pos = result.IndexOf(Environment.NewLine, 0, StringComparison.OrdinalIgnoreCase); Console.WriteLine(pos); // 1 result = Environment.NewLine; pos = result.IndexOf(Environment.NewLine); Console.WriteLine(pos); // 0 result = "test\n"; pos = result.IndexOf(Environment.NewLine); Console.WriteLine(pos); // 4 result = "test\r\n"; pos = result.IndexOf(Environment.NewLine); Console.WriteLine(pos); // -1 } } } </pre> <br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1480
(왼쪽의 숫자를 입력해야 합니다.)