PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

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

 August 29, 2022     c#, csv, visual-studio, webforms     No comments   

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 - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing