Replace characters.

S

shapper

Hello,

I need to do the following replacements on a string:

"á", "ã", "à" by "a"
"é", "è" by "e"
"í", "ì" by "i"
"ç" by c

Because when I use these characters on a file name I get something
similar to:
This%20n%c3%a3o%20%c3%a9%20%c3%a0%20ca%c3%a7a.pdf

Do I need to replace each character on a string or is there any
function that does this?

Thanks,
Miguel
 
J

Jeroen Mostert

shapper said:
I need to do the following replacements on a string:

"á", "ã", "à" by "a"
"é", "è" by "e"
"í", "ì" by "i"
"ç" by c

Because when I use these characters on a file name I get something
similar to:
This%20n%c3%a3o%20%c3%a9%20%c3%a0%20ca%c3%a7a.pdf
That's URL/URI escaping, so your story is not complete. URI paths and file
names are different things. If you do:

File.WriteAllText("áãàéèíìç.txt", "Windows has no problems with this.");

You should see a file with the name specified.
Do I need to replace each character on a string or is there any
function that does this?
To replace multiple characters at once, you can use Regex.Replace(), but I
strongly suspect that that's not what you need to do here.

You may need to use Uri.UnescapeDataString(), or you might need to find a
different way of getting the file name in the first place. I can't tell from
here.
 
S

shapper

That's URL/URI escaping, so your story is not complete. URI paths and file
names are different things. If you do:

   File.WriteAllText("áãàéèíìç.txt", "Windows has no problems with this.");

You should see a file with the name specified.


To replace multiple characters at once, you can use Regex.Replace(), but I
strongly suspect that that's not what you need to do here.

You may need to use Uri.UnescapeDataString(), or you might need to find a
different way of getting the file name in the first place. I can't tell from
here.

I am using the following on and ASP.NET MVC Controller:

public ActionResult Download(Guid id) {
var myfile = (from f in database.Files
where f.FileId == id
select f}).SingleOrDefault();

string path = Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, myfile.Path);
FileStream file = System.IO.File.OpenRead(path);
string name = String.Concat("(MyWebSite) ", file.Title,
Path.GetExtension(path));

// Fix filename only in IE
HttpContext.Response.AddHeader("Content-Disposition",
"attachment; filename=" + Server.UrlPathEncode(name));

return new BinaryStreamResult(file);
}

The fix I added makes something like the file "file ão à.pdf" to be
downloaded exactly with that name ... but with Firefox it is not
working!

I don't know what else to try ... So I am considering converting
spaces to _ and characters like Á,á,à,À,ã,Ã to a and so on ...

What should I do?

Thanks,
Miguel
 
H

Hans Kesting

shapper expressed precisely :
Hello,

I need to do the following replacements on a string:

"á", "ã", "à" by "a"
"é", "è" by "e"
"í", "ì" by "i"
"ç" by c

Because when I use these characters on a file name I get something
similar to:
This%20n%c3%a3o%20%c3%a9%20%c3%a0%20ca%c3%a7a.pdf

Do I need to replace each character on a string or is there any
function that does this?

Thanks,
Miguel

If you still want to do this (after the other answers), here is a way:

public static String ToBasicASCII(String s)
{
return new String(
s.Normalize(System.Text.NormalizationForm.FormD).ToCharArray()
.Where(c => System.Globalization.CharUnicodeInfo
.GetUnicodeCategory(c) !=
System.Globalization.UnicodeCategory.NonSpacingMark)
.ToArray());
}

Hans Kesting
 
J

Jeff Johnson

If you still want to do this (after the other answers), here is a way:

public static String ToBasicASCII(String s)
{
return new String(
s.Normalize(System.Text.NormalizationForm.FormD).ToCharArray()
.Where(c => System.Globalization.CharUnicodeInfo
.GetUnicodeCategory(c) !=
System.Globalization.UnicodeCategory.NonSpacingMark)
.ToArray());
}

I assume you need to be using Framework 3.x for this to work...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top