从 NSString 创建安全文件名?

发布于 2024-11-28 22:34:30 字数 458 浏览 1 评论 0原文

有没有办法获取 NSString 并将其转换为安全版本,可以用作文件名保存到 iPhone 上的用户文档目录中。

我目前正在做这样的事情:

NSString *inputString = @"This is sample text which may have funny chars and spaces in.";

NSInteger len = [inputString length];        
NSString *newFilename = [[inputString substringToIndex:MIN(20, len)] stringByAppendingPathExtension:@"txt"];

这目前给我留下了类似的东西:

This is sample text .txt

需要确保文件名中不允许的字符也被删除。

Is there way to take a NSString and turn it into a safe version that can be used as a filename to save to the user Documents directory on the iPhone.

I'm currently doing something like this:

NSString *inputString = @"This is sample text which may have funny chars and spaces in.";

NSInteger len = [inputString length];        
NSString *newFilename = [[inputString substringToIndex:MIN(20, len)] stringByAppendingPathExtension:@"txt"];

This currently leaves me with something like:

This is sample text .txt

Need to make sure characters that are not allowed in filenames and stripped out too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じ违心 2024-12-05 22:34:31

你可能最终会得到一些正则表达式或其他东西。仅仅是因为使用 except 过滤器太危险了(您可能会错过一些非法字符)。

因此,我建议您使用 RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/),并结合以下代码行:

inputString = [inputString stringByReplacingOccurencesOfRegex:@"([^A-Za-z0-9]*)" withString:@""];

这将替换除 AZ、az 和 0-9 =) 之外的所有字符!

You'll probably end up with some regex or something. Simply because it's too dangerous to use a except-filter (you may miss some illegal chars).

Therefor I'ld recommend you to use RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/), combined with the following line of code:

inputString = [inputString stringByReplacingOccurencesOfRegex:@"([^A-Za-z0-9]*)" withString:@""];

This will replace all characters except A-Z, a-z and 0-9 =)!

心如荒岛 2024-12-05 22:34:31

您也可以用老式的方式来代替使用正则表达式:

NSString* SanitizeFilename(NSString* filename)
{
    NSMutableString* stripped = [NSMutableString stringWithCapacity:filename.length];
    for (int t = 0; t < filename.length; ++t)
    {
        unichar c = [filename characterAtIndex:t];

        // Only allow a-z, A-Z, 0-9, space, -
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 
        ||  (c >= '0' && c <= '9') || c == ' ' || c == '-')
            [stripped appendFormat:@"%c", c];
        else
            [stripped appendString:@"_"];
    }

    // No empty spaces at the beginning or end of the path name (also no dots
    // at the end); that messes up the Windows file system.
    return [stripped stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

You can also do it the old fashioned way instead of using a regex:

NSString* SanitizeFilename(NSString* filename)
{
    NSMutableString* stripped = [NSMutableString stringWithCapacity:filename.length];
    for (int t = 0; t < filename.length; ++t)
    {
        unichar c = [filename characterAtIndex:t];

        // Only allow a-z, A-Z, 0-9, space, -
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') 
        ||  (c >= '0' && c <= '9') || c == ' ' || c == '-')
            [stripped appendFormat:@"%c", c];
        else
            [stripped appendString:@"_"];
    }

    // No empty spaces at the beginning or end of the path name (also no dots
    // at the end); that messes up the Windows file system.
    return [stripped stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
堇年纸鸢 2024-12-05 22:34:31

如果您真正需要的只是一个安全的随机文件名,那么只需使用 SecRandomCopyBytes 达到您想要的长度并对其进行 base64 编码即可。

If all you really need is a safely random filename then just use SecRandomCopyBytes to the length you want and base64-encode it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文