Substring with range out of bounds?

前端 未结 4 1643
一生所求
一生所求 2020-12-20 17:29

Okay, so this is a bit confusing (well to me). I have a string that has a single number I want out of it. I have surrounded this number with \'/\' so that I will be able to

4条回答
  •  旧巷少年郎
    2020-12-20 17:46

    I think you have confusion in syntax of NSMakeRange. It is something like this

    NSMakeRange(<#NSUInteger loc#>, <#NSUInteger len#>)
    

    <#NSUInteger loc#>: It is the location from where you want to start picking or substring.

    <#NSUInteger len#>: This is the length of your output or substring.

    Example:

    Mytest12test

    Now I want to pick'12'

    so:

    NSString *t=@"Mytest12test";
    NSString *x=[t substringWithRange:NSMakeRange(6, 2)] ;
    

    In your code instead of length you are passing index of end character that is your mistake.

提交回复
热议问题