Split String in C#

前端 未结 9 988
萌比男神i
萌比男神i 2020-12-28 09:10

I thought this will be trivial but I can\'t get this to work.

Assume a line in a CSV file: \"Barack Obama\", 48, \"President\", \"1600 Penn Ave, Washington DC

相关标签:
9条回答
  • 2020-12-28 09:55

    You should be using Microsoft.VisualBasic.FileIO.TextFieldParser for that. It will handle all the CSV stuff correctly for you, see: A similar question with example using the TextFieldParser

    PS: Do not fear using the Microsoft.VisualBasic dll in a C# project, it's all .NET :-)

    0 讨论(0)
  • 2020-12-28 09:56

    Can't you change how the CSV is generated? Using OpenOffice, you can set the char separator (use ;) and how the string is delimited (using " or ').

    It would be like this: 'President';'1600 Penn Ave, Washington DC'

    0 讨论(0)
  • 2020-12-28 09:59

    string temp = line.Replace( "\"", "" );

    string[] tokens = temp.Split(',')

    0 讨论(0)
提交回复
热议问题