Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:08:26+00:00 2026-05-29T21:08:26+00:00

Is there some standard method to remove all characters from string that cannot be

  • 0

Is there some standard method to remove all characters from string that cannot be used for a filenames in osx? Does the characters you are allowed to use correspond to some commonly used category? (I know defining a list of allowed characters would be possible, but I want to be able to use filenames from several languages (chinese, among others), so this will be a bit of a pain.) A solution that works in ubuntu too, would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:08:28+00:00Added an answer on May 29, 2026 at 9:08 pm

    I use the following code to convert a arbitrary string into a valid filename. It follows similar rules (if not the same) to those applied to creating a webloc file when dragging a URL to the Desktop.

    // Returns a sting representing this filename transformed (if neccesary) to make
    // a valid (Mac OS X) filename.
    - (NSString *) stringByMakingFileNameValid:(NSString *)fileName
    {
        NSMutableString * validFileName = [NSMutableString stringWithString:fileName];
        if (!validFileName || [validFileName isEqualToString:@""]) {
            return @"untitled";
        }
        // remove initial period chars "."
        if ([validFileName hasPrefix:@"."]) {
            NSRange dotRange = [validFileName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
            [validFileName deleteCharactersInRange:dotRange];
        }
        // remove any colon chars ":" (same as webloc creation behaviour)
        [validFileName replaceOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange(0, [validFileName length])];
        // this may lead to spaces at either end which need trimming
        validFileName = [[[validFileName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] mutableCopy] autorelease];
    
        // if there is nothing left return default value
        if ([validFileName isEqualToString:@""]) {
            return @"untitled";
        }
        // replace other disallowed Mac OS X characters
        [validFileName replaceOccurrencesOfString:@"/" withString:@"-" options:0 range:NSMakeRange(0, [validFileName length])];
    
        // if grater than 102 chars reduce to 101 and add elipses
        if ([validFileName length] > 102) {
            [validFileName deleteCharactersInRange:NSMakeRange(100, [validFileName length]-100)];
            [validFileName appendString:@"…"];
        }
    
        return [[validFileName copy] autorelease];
    }
    

    At the Darwin level “/” characters are stored as “:” characters (and vice versa). They are removed here for cosmetic reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a cocoa app of mine, there is a method that does some operations
Is there some standard way or has anyone written something that allows you to
is there some standard about how to graphically represent database schemas? Is UML a
Is there a way to use DecimalFormat (or some other standard formatter) to format
Is there any standard way of implementing some sort of a write-through buffer for
Are there standard file names for Terms of Use and Privacy Policies? For some
Are there some standards that you consider to be so obvious that they would
i know that there is some rules and standards in css handling but i
I know that there are so many standards for writing code. And some policy
My application has some features that are accessible to all users, and some other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.