2014-04-12 1 views
1

나는 그들이 내가 가진 문제를 가지고 가정 (자신의 문제에 도움을 필요로 다른 사람을 위해 여기에 코드를 가져온 거에요 를 수정했습니다. IRC 채팅 명령 구문 분석


FIXED CODE THAT WORKS 

    public static bool CommandExists(String value) 
    { 
     string[] commands = File.ReadAllText("commands.txt") 
         .Split() 
         .Where(x => x.StartsWith(value)) 
         .Distinct() 
         .ToArray(); 
     return commands.Contains(value); 
    } 
    public static string CommandParse(String value, string nick) 
    { 
     IEnumerable<string> lines; 
     lines = File.ReadLines("commands.txt"); 
     IEnumerable<string> command = lines 
      .Where(d => d.StartsWith(value, 
       StringComparison.CurrentCultureIgnoreCase)); 
     foreach (string line in command) { 
      string vals = line 
       .Replace("@nick", nick) 
       .Replace("@upnick", nick.ToUpper()) 
       .Replace(value + " ", ""); 
      return vals; 
     } 
     return null; 
    } 
그래서 내가 봤는데 몇 시간 동안 노력하고 나는 주변을 둘러 보았고 내가하려는 일과 관련된 것을 찾을 수 없었다.

나는 "commands.txt"라는 텍스트 파일을 가지고 있는데 나는 노력하고있다. 텍스트를 파싱합니다. 내용은 다음과 같습니다.


!ver Testing this

!help Hello, current commands are: !help, !ver지금 나는 '배열의 범위를 벗어난 인덱스'를 얻을

string x1 = File.ReadAllLines("commands.txt").ToString(); 
string[] x2 = x1.Split(' '); 
string x3 = x2[0]; 
Console.WriteLine(x3); 

당기면. 나는 내가 뭘 잘못하고 있는지 전혀 모른다. 명령이있는 경우 또한 호출 할 '정적 부울'를 사용하기 위해 노력하고있어 지금까지 내가

public static bool CommandExists(String value) 
{ 
    if (File.ReadAllLines("commands.txt").Contains(value.ToString())) { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

을 가지고 그뿐만 아니라 작동하지 않습니다.

해당 예외의 원인은 무엇입니까?

편집 : CommandParse()

public static string CommandParse(string value, string nick) 
    { 
     string[] commands = File.ReadAllText("commands.txt") 
       .Split() 
       .Where(x => x.StartsWith("!"+value.ToLower())) 
       .Distinct() 
       .ToArray(); 
     string cmd = commands[1] 
      .Replace("@nick", nick) 
      .Replace("@nickup", nick.ToUpper()); 
     return cmd; 
    } 

가 지금은 True를 반환 것을 알고 어떻게 그렇지 명령 자체를 true를 반환하지만 반환받을 수 있나요

+0

포럼 사이트와 달리, 우리는 "감사"또는 "도움을 주셨으면"또는 [그래서]의 서명을 사용하지 않습니다. [Hi, 감사, 태그 라인 및 인사말을 게시물에서 삭제해야합니까?] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be- 게시물에서 삭제됨). – rene

답변

4

ReadAllLines 문자열의 배열을 반환, 당신을 ToString을 사용하고 줄을 가져 오는 대신 어떤 공백을 포함하지 않는 문자열 배열의 형식 이름을 얻으므로 Split(' ')은 아무 것도 변경하지 않습니다. 모든 텍스트를 읽으려면 ReadAllText 메서드를 사용하십시오.

string[] commands = File.ReadAllText("commands.txt") 
        .Split() 
        .Where(x => x.StartsWith("!")) 
        .Distinct() 
        .ToArray(); 

그런 다음 당신의 방법은 다음과 같이 표시됩니다 :

public static bool CommandExists(String value) 
{ 
    string[] commands = File.ReadAllText("commands.txt") 
        .Split() 
        .Where(x => x.StartsWith("!")) 
        .Distinct() 
        .ToArray(); 

    return commands.Contains(value); 
} 

하면

string x1 = File.ReadAllText("commands.txt"); 

것 같다 모든 명령은 그래서 당신은이 같은 배열로 모든 명령을 얻을 수 !로 시작 제외하려면 시작 부분에 !을 추가하십시오.다음에 .Select(x => x.TrimStart('!'))을 추가하십시오..

+0

이제 명령을 구문 분석하면 까다로운 부분이 아닙니까? 어떻게 그 사용에 대해 갈 것입니다 정적 문자열 CommandParse (문자열 값) {} –

+0

편집 : 위의 CommandParse() 추가, 작동합니까? –