ios 如何从 NSString 中捕获最后 4 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6591538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to capture last 4 characters from NSString
提问by C.Johns
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
.
我正在接受来自 aNSString
的随机数并将其传递给我正在创建的方法,该方法将仅捕获.size
UITextField
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
我浏览了NSString
类参考库,我发现唯一一个真正的选项看起来像我想要的那样
- (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.
到目前为止,这是我创建的方法,它是NSString
从其他地方的 IBAction传递的。
- (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?
}
回答by KingofBliss
Use the substringFromIndex method,
使用 substringFromIndex 方法,
OBJ-C:
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))
回答by Yash Joshi
Try This,
尝试这个,
NSString *lastFourChar = [yourNewString substringFromIndex:[yourNewString length] - 4];
回答by CodeBender
The Swift answer provided by KingOfBliss is producing an error for me as of XCode 7.3, and should not be used going forward due to bridging to NS classes, which should be removed in Swift 3.0.
KingOfBliss 提供的 Swift 答案从 XCode 7.3 开始对我产生错误,由于桥接到 NS 类,因此不应继续使用,应在 Swift 3.0 中删除。
A better example that considers an invalid range, as well as the possibility of special characters to provide the right spot to remove:
一个更好的例子,考虑了无效范围,以及特殊字符提供正确删除位置的可能性:
The string in question: Voulez-vous un cafe??
有问题的字符串:Voulez-vous un cafe??
var trimmedString = "Voulez-vous un caf\u{65}\u{301}?"
let stringSize = trimmedString.characters.count
let startIndex = 4
if stringSize >= startIndex {
let range = trimmedString.endIndex.advancedBy(-startIndex)..<trimmedString.endIndex
trimmedString.removeRange(range)
}
This will produce an answer of Voulez-vous un c
.
这将产生 的答案Voulez-vous un c
。
By accessing the string with the start & end index values, you protect against situations where not all chars are the same size (ex: \u{65}\u{301}
)
通过访问与起始和结束的索引值的字符串,防止因为不是所有的字符大小相同的情况下(例如:\u{65}\u{301}
)