none
a string does not recognize CrLf RRS feed

  • Question

  • hi everybody

    I have an Foreach with a List of strings, and compares if the line have only have CrLF just check if is !="" do some struff... like this...

    foreach(string line in ListStrings)
    {
    	if(line !="")
    	{
              do some stuff...
            }else
               line.Replace("", "*****");// here is the error
    }
    in the line.replace show this==> "String cannot be of zero length", but if a replace for CrLf line.Replace(@"\r\n","***") it does nothing!!



    • Edited by greg_dorian Wednesday, December 19, 2018 1:30 PM
    Wednesday, December 19, 2018 1:21 PM

Answers


  •  I can not find a way to replace when a line comes with a single \ r \ n, only when you compare a "" line?

    List<string> ls = new List<string>();
    ls.Add("Line1");
    ls.Add("Line2");
    ls.Add("");
    ls.Add("");
    ls.Add("Line3");
    
    for(int n = 0; n<ls.Count; ++n)
    {
        if (ls[n] == "")
        {
            ls[n] = "****";
        }
        Console.WriteLine(ls[n]);
    }
    

    - Wayne

    • Marked as answer by greg_dorian Wednesday, December 19, 2018 3:35 PM
    Wednesday, December 19, 2018 2:47 PM

All replies

  • Seems that you execute line.Replace(@"\r\n","***") when line is empty (detected by if). Since it is empty, there is nothing to replace. In addition, you should remove @.


    Wednesday, December 19, 2018 1:43 PM
  • Parece que ejecutas line.Replace (@ "\ r \ n", "***")cuando la línea está vacía (detectada por si). Ya que está vacío, no hay nada que reemplazar. Además, debes eliminar @.


    you're right! but I can not find a way to replace when a line comes with a single \ r \ n, only when you compare a "" line?
    • Edited by greg_dorian Wednesday, December 19, 2018 2:26 PM
    Wednesday, December 19, 2018 2:12 PM

  •  I can not find a way to replace when a line comes with a single \ r \ n, only when you compare a "" line?

    List<string> ls = new List<string>();
    ls.Add("Line1");
    ls.Add("Line2");
    ls.Add("");
    ls.Add("");
    ls.Add("Line3");
    
    for(int n = 0; n<ls.Count; ++n)
    {
        if (ls[n] == "")
        {
            ls[n] = "****";
        }
        Console.WriteLine(ls[n]);
    }
    

    - Wayne

    • Marked as answer by greg_dorian Wednesday, December 19, 2018 3:35 PM
    Wednesday, December 19, 2018 2:47 PM