1

I developing a xamarin form app and I am assigning the current date time as a Filename for image. Currently the image is saved as "7202017 53150 PM.jpg". I want it to be saved like this "720201753150PM.jpg". How can I remove the space between the date and time?

I tried like below but it did not work.

string _imagename = DateTime.Now.ToString();
_imagename.Replace(" ", string.Empty);
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
batwing
  • 257
  • 1
  • 8
  • 22
  • 5
    It should be `_imagename = _imagename.Replace(" ", string.Empty);` for the second line. – Peter Bons Jul 20 '17 at 09:44
  • 4
    `"7202017 53150 PM.jpg"` will never be the result of `DateTime.Now.ToString()`. Always show the real code. Instead of replacing spaces, why don't you generate the correct string in the first place by modifying the format? – Tim Schmelter Jul 20 '17 at 09:46

4 Answers4

7

Actually the String.Replace() Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string(in short it won't change the actual string) So you need to assign the result to another variable. And perform the replace operation there.

But Why you go for another replace? whynot use .ToString() like the following?

string format = "Mddyyyyhhmmsstt";
string _imagename = String.Format("{0}.jpg",DateTime.Now.ToString(format))
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

You need to assign the new value as string is immutable:

string _imagename = DateTime.Now.ToString();

_imagename = _imagename.Replace(" ", string.Empty);
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • 1
    Personally I upvoted but perhaps the downvoter thinks the string should have been generated without spaces rather than the space removed afterwards. This does answer the question though and tells the OP exactly what he did wrong. – Chris Jul 20 '17 at 09:48
  • Haven't downvoted. But instead of showing how to replace spaces with empty-string(where you can find plenty duplicates), OP should not create the wrong date-time string in the first place. – Tim Schmelter Jul 20 '17 at 09:48
  • thank you, I have developed application to upload image to server but don't how to remove space..lol. – batwing Jul 20 '17 at 09:49
  • You're both right! This just solves an unnecessary problem. Better to generate a proper string in the first place, like in the answer of un-lucky. But in the case, that OP can't change the creation of the input string this will help – Romano Zumbé Jul 20 '17 at 09:51
  • Yup. I think the biggest reason this answer is helpful is because it addresses the problem of the OP not assigning the result of his replace. This is an important thing to learn even if it isn't the best way to do what they are doing. – Chris Jul 20 '17 at 09:54
1

If you want to order by file name I'd suggest to use a notation like yyyyMMddHHmmss. That way, with increasing date/time the sort order will also increase.

Other than that, strings are immutable in c#. Thus calling Replace does not change the original string. You need to assign the result to your variable as @Romano Zumbé pointed out.

You can just use a format string like the following (including the sorting suggestion):

string imagename = $"{DateTime.Now:yyyyMMddHHmmss}.jpg";

otherwise it would be:

string imagename = $"{DateTime.Now:Mddyyyyhmmsstt}.jpg";
Adwaenyth
  • 2,020
  • 12
  • 24
1

This is fastest way I know:

Regex.Replace(_imagename, @"\s+", "")

Looking at your string ill also suggest replace spaces with a empty string. And you could do it by applying built in Replace method:

    string _imagename = DateTime.Now.ToString();

   _imagename = _imagename.Replace(" ", string.Empty);
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102