如何从 NSString 中捕获最后 4 个字符

发布于 2024-11-18 18:49:12 字数 869 浏览 4 评论 0原文

我从 UITextField 接受随机 sizeNSString 并将其传递给我正在创建的方法,该方法仅捕获最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我们只是彼此的过ke 2024-11-25 18:49:12

使用 substringFromIndex 方法,

OBJ-C:

NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-4, 0)]; //in case string is less than 4 characters long.

SWIFT:

let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-4,0))

Use the substringFromIndex method,

OBJ-C:

NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-4, 0)]; //in case string is less than 4 characters long.

SWIFT:

let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-4,0))
旧夏天 2024-11-25 18:49:12

试试这个,

NSString *lastFourChar = [yourNewString substringFromIndex:[yourNewString length] - 4];

Try This,

NSString *lastFourChar = [yourNewString substringFromIndex:[yourNewString length] - 4];
哆兒滾 2024-11-25 18:49:12

你可以在 Swift 5 中检查这个函数:

func subString(from myString: NSString, length: Int) {
    let myNSRange = NSRange(location: myString.length - length, length: length)
    print(myString.substring(with: myNSRange))
}
subString(from: "Menaim solved the issue", length: 4) // Output: ssue

You can check this function in Swift 5:

func subString(from myString: NSString, length: Int) {
    let myNSRange = NSRange(location: myString.length - length, length: length)
    print(myString.substring(with: myNSRange))
}
subString(from: "Menaim solved the issue", length: 4) // Output: ssue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文