Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete character out of string

Tags:

string

c#

io

csv

I am having some problems with a quite easy task - i feel like im missing something very obvious here.

I have a .csv file which is semicolon seperated. In this file are several numbers that contain dots like "1.300" but there are also dates included like "2015.12.01". The task is to find and delete all dots but only those that are in numbers and not in dates. The dates and numbers are completely variable and never at the same position in the file.

My question now: What is the 'best' way to handle this problem?

From a programmers point of view: Is it a good solution to just split at every semilicon, count the dots and if there is only one dot, delete it? This is the only way to solve the problem i could think of by now.


Example source file:

2015.12.01;
13.100;
500;
1.200;
100;

Example result:

2015.12.01;
13100;
500;
1200;
100;
like image 228
Lunatiic Avatar asked Apr 06 '16 09:04

Lunatiic


Video Answer


1 Answers

If you can rely on the fact that dates have two dots and numbers just one, you can use that as a filter:

string s = "123.45";
if (s.Count(x => x == '.') == 1)
{
    s = s.Replace(".", null);
}
like image 179
Patrick Hofman Avatar answered Nov 14 '22 23:11

Patrick Hofman