2013-02-24 5 views
4

다음 문제가 있습니다.string.Remove doesnt work

ive는 jpg 파일에 대한 추가 링크로 Google 이미지 검색을 사용하는 programm로 작성되었습니다. 하지만 링크 앞에 나는 내가 가질 수없는 15 자 긴 문자열이있다.

public const int resolution = 1920; 
    public const int DEFAULTIMGCOUNT = 40; 

    public void getimages(string searchpatt) 
    { 
     string blub = "http://images.google.com/images?q=" + searchpatt + "&biw=" + resolution; 
     WebClient client = new WebClient(); 

     string html = client.DownloadString(blub);            //Downloading the gooogle page; 
     MatchCollection mc = Regex.Matches(html, 
      @"(https?:)?//?[^'<>]+?\.(jpg|jpeg|gif|png)"); 

     int mccount = 0;                  // Keep track of imgurls 
     string[] results = new string[DEFAULTIMGCOUNT];           // String Array to place the Urls 

     foreach (Match m in mc)                 //put matches in string array 
     { 
      results[mccount] = m.Value;     
      mccount++; 
     } 

     string remove = "/imgres?imgurl="; 
     char[] removetochar = remove.ToCharArray(); 

     foreach (string s in results) 
     { 
      if (s != null) 
      { 
       s.Remove(0, 15); 
       Console.WriteLine(s+"\n"); 
      } 
      else { } 
     } 
     // Console.Write(html); 


    } 

나는 제거 및 trimstart를 시도했지만 그 중 아무 것도 작동하지 않으며 실패를 파악할 수 없습니다.

나는

 for (int i = 0; i < results.Count(); i++) 
     { 
      if (results[i] != null) 
      { 
       results[i] = results[i].Substring(15); 
       Console.Write(results[i]+"\n"); 
      } 
     } 
+0

당신은 당신이 문자열을 사용할 수있는 15 자 덤프 할 것을 알고 있기 때문에. –

+1

대부분의 코드는 실제로 귀하의 질문과 관련이 없으며 사용하지도 않는 변수가 있습니다. (왜'ToCharArray '를 호출하는 것입니까?) –

+0

내 실패가 MatchCollection에 있을지 확신 할 수 없었습니다 –

답변

24

처럼 해결 (I이 중복 확신 해요,하지만 난 바로 하나를 찾을 수 없습니다.) .NET에서

문자열은 불변이다. string.Remove, string.Replace 등의 메소드는 문자열의 내용을 변경하지 않습니다. 문자열을 반환합니다. 문자열은 기존 문자열을 반환합니다. 다른 방법

s = s.Remove(0, 15); 

또는 단지 Substring을 사용합니다 :

그래서 당신은 같은 것을 원하는

s = s.Substring(15); 
+0

감사합니다 :) 나는 그 사실을 알지 못했습니다. –