줄 바꿈으로 구분 된 문자열을 구문 분석하고 있습니다. 그것을 위해, 나는 가지고있다 :문자열이 특정 문자열로 시작하는 경우 어떻게 사전에 부분 문자열을 추가합니까?
for line in str.Split([|Environment.NewLine|], StringSplitOptions.None) do
나는 각 라인을 잘 준다. 내가 무엇을하고 싶은지 line
걸릴 및 특정 문자열로 시작하는 경우 키를 시작 문자열로 사전에 나머지를 추가하십시오. 기본적으로, 나는 F #으로 다음과 같은 논리를 구현하려는 :
if line equals "[end]" then
break
else if line startsWith "string1" then
add the rest of line to a dictionary with the key "string1"
else if line startsWith "string2" then
add the rest of line to a dictionary with the key "string2"
else
continue
내가 발견 한 바로는, 내가 대신 음역의이 작업을 수행하기 위해 match..with
문을 사용해야하는 F에 #, this의 라인을 따라.
지금까지 내가 가지고 : 내게 처리하는 방법을 모르겠어요 다음과 같은 오류, 제공
open System
open System.IO
open System.Collections.Generic
let str =
"Some string that
spans multiple lines
with prefixes
[end]"
let (|Prefix|_|) (p:string) (s:string) =
if s.StartsWith(p) then
Some(s.Substring(p.Length))
else
None
let dict = new Dictionary<string, string>();
for line in str.Split([|Environment.NewLine|], StringSplitOptions.None) do
match line with
| Prefix "[end]" -> dict // stop searching
| Prefix "spans" rest -> dict.Add("spans", rest) // add to dictionary
| _ ->() // continue searching
: 내가 MonoDevelop을 사용하고
test.fs(20,5): error FS0001: Type mismatch.
Expecting a string -> 'a option but given a string -> string -> string option
The type ''a option' does not match the type 'string -> string option'
을/오픈 소스 F 번호 컴파일러라면 아무 것도 중요하지 않겠지 만 그렇게 생각하지는 않습니다.
이리터럴 문자열이
let str =
"Some string that
spans multiple lines
with prefixes
[end]"
해야하거나, 그렇지 않으면 당신은 당신이
을하지 않으려는 각 라인의 시작 부분에 약간의 공간을 얻을 :
감사합니다. 'str' 비트는 단순한 예제를 만드는 것으로부터 나왔지만, 실제 코드의 어떤 지점에서 실수를 범할 것이라고 확신합니다. 그렇게 염두에 두겠습니다. – ipavl