iOS文件名中允许使用哪些字符? [英] What characters are allowed in a iOS file name?

查看:667
本文介绍了iOS文件名中允许使用哪些字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来确保字符串可以用作iOS下的文件名。我目前在删除不兼容字符的代码部分。

  NSString * filename = @A file name;我想知道是否正确。 
fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
fileName = [fileName stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

我也想知道是否已经有一个方法将字符串验证为文件名。 p>

感谢您的建议!

解决方案

重新使用错误的方法。修剪字符串只会删除字符串开头和结尾处的字符。



您正在查找的内容更像:

  fileName = [fileName stringByReplacingOccurrencesOfString:@/withString:@_]; 

然而,这是一个次优解决方案,因为你必须为每个你想要的字符排除,所以也许你想继续寻找或写你自己的方法来处理字符串。



iOS是基于UNIX的,因此我认为它支持几乎任何字符在文件名。 UNIX允许使用空格,<>,|,\,:,(,)和& ;;;以及通配符,例如?和*,使用\符号引用或转义。但是我不会在我的文件名中使用任何这些字符。实际上,我会将文件名中的字符限制为'a' - 'z','0' - '9','_'和'。'。


I'm looking for a way to make sure a string can be used as a file name under iOS. I'm currently in the section of the code that deletes incompatible characters. I'm wondering if I'm doing it right.

NSString *filename = @"A file name";
fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet controlCharacterSet]];
fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];

I'm also wondering if there's already a method that validates a string as a file name.

Thank you for your advice!

解决方案

First of all, you're using the wrong method. Trimming the string will only remove characters in the beginning and the end of the string.

What you're looking for is something more like:

fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@"_"];

However, that's a suboptimal solution, since you'll have to do that for every character you want to exclude, so maybe you want to keep looking or write you're own method for manipulating the string.

iOS is UNIX based and as such I suppose it supports almost any characters in filenames. UNIX allows white spaces, <, >, |, \, :, (, ), &, ;, as well as wildcards such as ? and *, to be quoted or escaped using \ symbol. However I wouldn't use any of those characters in my filenames. In fact, I would restrict the characters in my filenames to 'a'-'z', '0'-'9', '_' and '.'.

这篇关于iOS文件名中允许使用哪些字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆