0

let us consider this as a string--'Wi-Fi 1234ff'

i would like to trim this as -'wifi'

removing last 6 characters, special characters and space.

what i tried is to remove space-

NSString *trimmedString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

to remove special characters-by pointing ($) what character i want to remove

 NSCharacterSet *trimmedString = [NSCharacterSet characterSetWithCharactersInString:@"$"];
string = [string stringByTrimmingCharactersInSet:trimmedString];

But wondering how could i remove 'x' number of strings from the end.

something like this..

  if ([string length] > 0) {
string = [string substringToIndex:[string length] - x];
 } 
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Avis
  • 495
  • 4
  • 13

1 Answers1

0

If your goal is to keep everything from the start up to, but not including the 1st space, then try this:

NSRange spaceRange = [myString rangeOfString:@" "];
if (spaceRange.location != NSNOtFound) {
    NSString *trimmedString = [myString substringToIndex:spaceRange.location];
    // This gives you @"Wi-Fi"
}

If, instead, you want to find the last space, change the 1st line to:

NSRange spaceRange = [myString rangeOfString:@" " options:NSBackwardsSearch];

That will find the last space.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • is there anything else to get including 1st space..but removing last x number of characters.example--'Wi-Fi zone 1233ff' to 'Wi-fi Zone' – Avis Sep 22 '14 at 18:44