0

I have a string which comes as Filename [1234568]. I need to delete the square bracket part of the string and just get the filename. How can we retrieve just the file name?

5
  • what the OP you expect Commented Nov 9, 2017 at 11:42
  • Just the Filename Commented Nov 9, 2017 at 11:46
  • you mean you need Filename or 1234568 or Filename 1234568 Commented Nov 9, 2017 at 11:59
  • better you provide what will be sample input and what you expect in output... Commented Nov 9, 2017 at 12:15
  • Sample input would be Filename [12345678] and output should be Filename Commented Nov 9, 2017 at 12:23

1 Answer 1

1

Below Regex may help you

NSMutableString *str = [@"Filename [1234568]" mutableCopy];
NSRegularExpression *regex = [NSRegularExpression         
                              regularExpressionWithPattern:@"\\[.+?\\]"
                              options:NSRegularExpressionCaseInsensitive
                              error:NULL];

NSString *newStr = [regex replaceMatchesInString:str 
                      options:0 
                        range:NSMakeRange(0, [str length])  
                 withTemplate:@""];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.