Monday, August 29, 2022

[FIXED] How to edit CSV data in C# like 1234567 to 123-4567?

Issue

Im trying to edit a postcode data which comes as for example 1234567 and i want to put a dash between 123 and 4567 (e.g 123-4567) is it possible to do this within C# without editing the csv file itself?

Im using a generic for loop to read through the csv file but i want to edit it like i asked above.

                foreach(string line in csvData_){
                string[] col = line.Split(',');
                deliveryAddress.Text = col[0];
                deliveryPostcode.Text = col[1];

deliveryPostcode contains 1234567 for e.g.


Solution

To solve your problem, you can use the method Insert() of the string object. For example:

deliveryPostcode.Text = col[1].Length == 7 ? col[1].Insert(3, "-") : col[1];

You have to fit it for your needs, but this is the way I would use.

Here is the MSDN page of the String.Insert().



Answered By - Clement
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.