2

Well I have export to CSV script.

I export list of struct. I write with help of StringWriter. In struct field array foreach cycle I iterate through all properties and after every property I put ';'. In the end of line I put WriteLine().

So as output I have:

value1;value2;value3;

And I want:

value1;value2;value3

The question is : how to get what I want from what I get, or based on what I'v already made.

I have 3 ideas right now:

  1. The last 2 symbols in line should be something like ";\r(\n)" So replace this combination with nothing.
  2. Check if the property is last.
  3. Trim last(before newline) symbol in each row.
Jevgeni Smirnov
  • 3,787
  • 5
  • 33
  • 50

4 Answers4

6

Use String.Join to form each line for your output. It prevents you from having to check which term is last.

http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

var values = { "value1", "value2", "value3" };

string line = string.Join(";", values);

line will be

"value1;value2;value3"
Nate Heinrich
  • 1,805
  • 14
  • 14
3

try this code,

str.TrimEnd(';');
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
1

Usually I just delete the last character after the line is formed.

Lartem
  • 19
  • 2
1

I use a StringBuilder and just do:

var builder = new StringBuilder();
// ...
// add the text
// ...
builder.Length--;

This way I can avoid the string copy.

codekaizen
  • 26,990
  • 7
  • 84
  • 140