【问题标题】:c# detect new Linec# 检测新行
【发布时间】:2013-07-16 23:19:40
【问题描述】:

我正在尝试解析来自网络的数据并在换行符处分开;但是 indexof("\n") 没有捕捉到它,但是richTextBox 将所有内容显示在单独的行上。我做错了什么?

string substr = substr.Substring(substr.IndexOf("\n"));

【问题讨论】:

  • 'indexof("\n") is not catch it' 它返回的是 -1 还是错误的值?
  • 我该怎么做呢?
  • 没有赶上新行
  • 您能发布您要解析的内容吗? (或至少是 sn-p)

标签: c# visual-studio-2012 richtextbox newline


【解决方案1】:

为什么不直接使用

String.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);

并将其读入数组?这将像你想要的那样“在换行符处分隔它”

【讨论】:

  • 你分配给字符串数组了吗?
  • string[] 行 = substr.Split(System.Environment.NewLine);仍然有错误“'string.Split(params char[])' 的最佳重载匹配有一些无效参数
  • 尝试更改
  • @jister:string[] lines = substr.Split(); 带有空参数呢?这将使用任何空格作为其分隔符。这行得通吗?
【解决方案2】:

你有

string url = "google.com/finance/getprices?i="; 
     + period + "&p=" 
      + days + "d&f=d,o,h,l,c,v&df=cpct&q=" + ticker; 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
textBox_Ticker.Text = url; 
StreamReader sr = new StreamReader(response.GetResponseStream()); 
string data = sr.ReadToEnd(); 
sr.Close();

现在,我将重点关注最后 3 行,并按以下方式使用它们

var lines = new List<string>();
using(StreamReader sr = new StreamReader(response.GetResponseSteam())
{
   while((var line = sr.ReadLine()) != null)
   {
       lines.Add(line);
   } 
}

注意:这是未经测试的,我想不到

【讨论】:

    【解决方案3】:

    这对我有用。

    string toEmail ='your string here'    
    var lstToEmail = toEmail.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多