如何从 NSString 中捕获最后 4 个字符
我从 UITextField
接受随机 size
的 NSString
并将其传递给我正在创建的方法,该方法仅捕获最后 4 个在字符串
中输入的字符。
我已经浏览过 NSString 类参考库,我发现唯一真正的选择,看起来它会做我想要的事情,
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
我之前已经使用过一次,但静态参数“不会改变” ',但是对于这个实现,我想使用根据传入字符串的大小而变化的非静态参数。
到目前为止,这是我创建的方法,它从一个传递 NSString
IB在其他地方采取行动。
- (void)padString:(NSString *)funcString
{
NSString *myFormattedString = [NSString stringWithFormat:@"%04d",[funcString intValue]]; // if less than 4 then pad string
// NSLog(@"my formatedstring = %@", myFormattedString);
int stringLength = [myFormattedString length]; // captures length of string maybe I can use this on NSRange?
//NSRange MyOneRange = {0, 1}; //<<-------- should I use this? if so how?
}
I am accepting an NSString
of random size
from a UITextField
and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string
.
I have looked through NSString
Class Reference library and the only real option I have found that looks like it will do what I want it to is
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
I have used this once before but with static parameters 'that do not change', But for this implementation I am wanting to use non static parameters that change depending on the size of the string coming in.
So far this is the method I have created which is being passed a NSString
from an IBAction else where.
- (void)padString:(NSString *)funcString
{
NSString *myFormattedString = [NSString stringWithFormat:@"%04d",[funcString intValue]]; // if less than 4 then pad string
// NSLog(@"my formatedstring = %@", myFormattedString);
int stringLength = [myFormattedString length]; // captures length of string maybe I can use this on NSRange?
//NSRange MyOneRange = {0, 1}; //<<-------- should I use this? if so how?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 substringFromIndex 方法,
OBJ-C:
SWIFT:
Use the substringFromIndex method,
OBJ-C:
SWIFT:
试试这个,
Try This,
你可以在 Swift 5 中检查这个函数:
You can check this function in Swift 5: