CSV string handling

后端 未结 13 841
北荒
北荒 2020-12-28 16:44

Typical way of creating a CSV string (pseudocode):

  1. Create a CSV container object (like a StringBuilder in C#).
  2. Loop through the strings you want to ad
相关标签:
13条回答
  • 2020-12-28 17:21

    I've used this method before. The Length property of StringBuilder is NOT readonly so subtracting it by one means truncate the last character. But you have to make sure your length is not zero to start with (which would happen if your list is empty) because setting the length to less than zero is an error.

    public string ReturnAsCSV(ContactList contactList)
    {
        StringBuilder sb = new StringBuilder();
    
        foreach (Contact c in contactList)       
        { 
            sb.Append(c.Name + ",");       
        }
    
        if (sb.Length > 0)  
            sb.Length -= 1;
    
        return sb.ToString();  
    }
    
    0 讨论(0)
提交回复
热议问题