-2

I am working on a calculator App. The NSMutablestring is used for calculation E.g "5-3*8-(-1)/77". But the label can't display endless an NSMutablestring, so is there have any way to limit NSMutablestring's length? (not too long, I want the NSMutablestring's length to be less than 100).

robbie_c
  • 2,428
  • 1
  • 19
  • 28
manreden
  • 3
  • 2
  • Presumably entry is made using a `UITextField`, therefore: http://stackoverflow.com/questions/2523501/set-uitextfield-maximum-length – trojanfoe Jul 09 '13 at 10:45
  • possible duplicate of [iPhone SDK: Set Max Character length TextField](http://stackoverflow.com/questions/433337/iphone-sdk-set-max-character-length-textfield) – Max MacLeod Jul 09 '13 at 10:46

2 Answers2

1

You can get the first 100 characters of a string as follows:

NSString *first100chars = [myString substringToIndex:100];

However, it sounds like you need to prevent the user from actually entering a string this long, which is a different problem. The comments to your question give examples of other people asking similar questions (e.g. Set the maximum character length of a UITextField), I suggest you check those.

Community
  • 1
  • 1
robbie_c
  • 2,428
  • 1
  • 19
  • 28
0

This is a job for an NSFormatter subclass. That's exactly what it's for.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55