code里面ReadLine()赋给了string,结果如果下一行比上一行要短的话,那么输出就是下一行+(上一行-下一行)的字符串
写了一个clearString,也不好用,快被烦死了,谁来救救我
code如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Net;
namespace ConsoleApplication1
{
    class Program
    {
        static public string cleanString(String s)
        {
            if (s != null && s.Length > 0)
            {
                for(int i=1;i<s.Length;i++)
                {
                    s.Remove(s.Length - i);
                    s = s.Substring(0, s.Length - i);
                                }
            
            }
            return s;
        
        
        }
        static void Main(string[] args)
        {
            StreamReader city = File.OpenText("city.txt");
            StreamReader state=File.OpenText("state.txt");
            StreamReader street_suffix = File.OpenText("street-suffix.txt");
            String str_city;
            List<String> st_city= new List<string>();
            WebClient client=new WebClient();
            byte[] page = client.DownloadData("http://www.picturehistory.com/product/id/1937");
            string content=System.Text.Encoding.UTF8.GetString(page);
            Console.Write(content);
            Console.ReadKey();
                 Console.WriteLine("Reading data and insterting into a StringTable.");
            while ((str_city = city.ReadLine()) != null)
            {    str_city.TrimEnd();
            Regex myRegex = new Regex(str_city, RegexOptions.IgnoreCase);
                if(myRegex.IsMatch(content))
                {   
                    st_city.Add(str_city);
                 
                    Console.Write(str_city);
                    Console.ReadKey();
                   
          }
                str_city = cleanString(str_city);            }            foreach (string cityItem in st_city)
            {
                Console.Write(cityItem);
                Console.ReadKey();            }
            String str_state;
            List<String> st_state = new List<string>();            Console.WriteLine("Reading data and insterting into a StringTable.");
            while ((str_state = state.ReadLine()) != null)
            {
                str_state.TrimEnd();
                String regexState = "\b" + str_state + "\b";
                Regex myRegex = new Regex(regexState, RegexOptions.IgnoreCase);
                if (myRegex.IsMatch(content))
                {
                    st_state.Add(str_state);
                    Console.Write(str_state);
                    Console.ReadKey();
                }
     
            }            String str_street;
            List<String> st_street = new List<string>();             Console.WriteLine("Reading data and insterting into a StringTable.");
            while ((str_street = street_suffix.ReadLine()) != null)
            {
                str_street.TrimEnd();
                String regexStreet="\b\\w+(?="+str_street+"\b)";
                
                Regex myRegex = new Regex(regexStreet, RegexOptions.IgnoreCase);
                if (myRegex.IsMatch(content))
                {
                    st_street.Add(str_street);
                    Console.Write(str_street);
                    Console.ReadKey();
                }
            }        }
    }
}

解决方案 »

  1.   

    用一个全局变量记录上一个记录就可以了啊
    string tcity="";static public string cleanString(string s)
    {
       string rest="";
       if(s.Lenght>0)
       {
          if(tcity=="")
          {
             tcity=s;
             rest=s;
          }else{
             if(s.Length<tciyt.Length)
             {
                rest=s+tcity.SubString(s.Lenght);
                tcity=s;
              }else{
                rest=s;
                tcity=s;
              }
          }
       }
       return rest;
    }
      

  2.   

    完全没明白。str_city = cleanString(str_city); 换成str_city = string.Empty;不行吗?
      

  3.   

    我用你的代码试了一下,不使用你的那个 cleanString 方法什么问题也没有。而且,你的 cleanString 方法本身逻辑也有问题
      

  4.   

    是我的console.write应该换成console.writeline,谢谢大家,我结贴了