Deleting a line in a text file

前端 未结 2 828
南笙
南笙 2021-01-27 09:25

Ok, I tried to delete lines that contain -- from a file. The file looks like this:

  -----------------------------------------------------------------
  Lorem ip         


        
2条回答
  •  鱼传尺愫
    2021-01-27 09:40

    So whats going on?

    Something like this:

    line = "-----------------------------------\n"
     => "-----------------------------------\n"
    
    a = line
     => "-----------------------------------\n"
    
    line.delete a
     ArgumentError: invalid range "--
     " in string transliteration
    

    The error raised by the delete method is because dashes are used for special character-range syntax:

    "hellob".delete "a-e"
     => "hllo"
    

    Probably you want to be taking a different approach to solve your goal, but it is difficult to tell what you want, as you don't show what you want to do with the data once lines are removed. Simply processing the line variable just means you have an altered String at that point.

提交回复
热议问题