2

I have a string which gives the Date (below)

NSString*str1=[objDict objectForKey:@"date"];

 NSLog(@" str values2%@",str1); --> 04-Jan-13

Now Problem is I need to Trim the"-13" from here .I know about NSDateFormatter to format date.but I can't do that here.I need to trim that

For that I am using:-

 NSCharacterSet *charc=[NSCharacterSet characterSetWithCharactersInString:@"-13"];

 [str1 stringByTrimmingCharactersInSet:charc];  

But this does not work.this does not trim...how to do that..help

Christien
  • 1,213
  • 4
  • 18
  • 32

5 Answers5

4

Not sure why not use an NSDateFormatter but here's a very specific way to approach this (very bad coding practice in my opinion):

NSString *theDate = str1;
NSArray *components = [theDate componentsSeparatedByString:@"-"];
NSString *trimmedDate = [NSString stringWithFormat:@"%@-%@",[components objectAtIndex:0],[components objectAtIndex:1]];
Stavash
  • 14,244
  • 5
  • 52
  • 80
3

Something like this:

NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

or this:

NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-13"];
Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
3

what you have done is correct. Only thing is stringByTrimmingCharactersInSet returns NSString. So you need to assign this value to NSString, like

str1 = [str1 stringByTrimmingCharactersInSet:charc];
arthankamal
  • 6,341
  • 4
  • 36
  • 51
3

But this does not work.this does not trim...

It does trim, but since NSString is immutable, the trimmed string is thrown away, because you do not assign it to anything.

This would work (but do not do it like that!)

str1 = [str1 stringByTrimmingCharactersInSet:charc];

What you do is not trimming, it's taking a substring. NSString provides a much better method for that:

str1 = [str1 substringToIndex:6]; // Take the initial 6 characters
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @Christien No, it does not :( At least, it does not *always* work: try `13-jan-13` to see why. – Sergey Kalinichenko Jan 04 '13 at 11:29
  • @Christien No, it does not work, because the order of characters in `NSCharacterSet` does not matter. Try it - it wouldn't work even with `31-jan-13`. – Sergey Kalinichenko Jan 04 '13 at 11:55
  • ya thanks for sharing this ..thanks.. now using substringToIndex – Christien Jan 04 '13 at 12:00
  • and one more thing..I need to open my app with Facebook login..which one I should use..I tried FBConnect but its been expired..then I tried Facebook SDK but it takes me to safari to open ..isnt it anything like FBConnect?? – Christien Jan 04 '13 at 12:03
  • @Christien I think you should ask this in a separate question, to get attention of someone who knows about FB (I do not even have an account there). – Sergey Kalinichenko Jan 04 '13 at 13:08
1

if you're sure you have your string always formatted like "NN-CCC-NN" you can just trim the first 6 chars:

NSString* stringToTrim = @"04-Jan-13";
NSString* trimmedString =  [stringToTrim substringToIndex:6];
NSLog(@"trimmedString: %@", trimmedString); // -> trimmedString: 04-Jan
meronix
  • 6,175
  • 1
  • 23
  • 36