Raj More
Raj More

Reputation: 48034

String Encoding

I am checking for the existence of a file in the XML backup of my iTunes library.

How do I change this string

E:\Entertainment\Music\Latin\100% Azucar The Best of Celia Cruz & La Sonora Matancera/08 Mi Soncito.mp3

to this

E:/Entertainment/Music/Latin/100%25%20Azucar%20%20The%20Best%20of%20Celia%20Cruz%20&%20La%20Sonora%20Matancera/08%20Mi%20Soncito.mp3

Changes:

  1. Space becomes %20
  2. Backslash becomes Forward-slash
  3. % (percentage sign) became %25

Upvotes: 1

Views: 1217

Answers (5)

Vilx-
Vilx-

Reputation: 107000

I think I found the key part of his problem. It's kinda hidden there, being the first sentence and all, so nobody noticed it much. XD

I am checking for the existence of a file in the XML backup of my iTunes library.

It seems to me that we are dealing with some rogue iTunes-XML encoding, which has messed up a bit somewhere along the line. My advice would be to go the other way round - decode the iTunes string (first through HtmlDecode, then UrlDecode) and then compare it to the original string you are looking for. This should be able to cope with most mis-encodings of iTunes.

Upvotes: 4

Aaron D
Aaron D

Reputation: 5886

A very simple route:

filepath.Replace("\", "/").Replace("%", "%25").Replace(" ", "%20").Replace("&", "&")

Upvotes: 2

Steven Sudit
Steven Sudit

Reputation: 19630

Looks like two separate things:

1) Replacing backslash with forward slash.

2) Applying URI-encoding.

The first is obvious. For the second, you have some choices, including Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode.

edit

3) As Vilx noticed, there seems to be HtmlEncoding of the filename portion.

Maybe the best answer is to use the Uri class to slice and dice the parts, encoding them as needed.

Upvotes: 7

Jeff LaFay
Jeff LaFay

Reputation: 13350

You do it with Uri.EscapeDataString() static method. Super easy.

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

%20 means space character. i see that you want to do something more with this string, so may i ask you what tools you'll prefer to use? regex? string replaces?

Upvotes: 0

Related Questions