substringFromIndex 和 substringToIndex用法

本文介绍了在Objective-C中如何使用NSString的方法substringFromIndex和substringToIndex来截取字符串的具体用法。通过实例展示了从指定索引开始截取直至字符串结尾,以及从字符串开头截取至指定索引前的子串。
部署运行你感兴趣的模型镜像
NSString *testsubstringToorFromIndex = @"test1234";
    NSString *fromIndexstring = [testsubstringToorFromIndex substringFromIndex:2];
    
    NSString *toIndexstring = [testsubstringToorFromIndex substringToIndex:2];
    NSLog(@"subtringfromindex is %@",fromIndexstring);
    NSLog(@"subtringToindex is %@",toIndexstring);

输出结果

2014-11-29 06:23:59.634 sdktest[587:11024] subtringfromindex is st1234

2014-11-29 06:23:59.634 sdktest[587:11024] subtringToindex is te


- (NSString *)substringFromIndex:(NSUInteger)anIndex
Description
Returns a new string containing the characters of the receiver from the one at a given index to the end.

返回一个字符串,这个字符串截取接受对象字符串范围是给定索引index到这个字符串的结尾


- (NSString *)substringToIndex:(NSUInteger)anIndex
Description
Returns a new string containing the characters of the receiver up to, but not including, the one at a given index.

返回一个字符串,这个字符串截取接受对象字符穿范围是从索引0到给定的索引index

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

- (void)loginQROpenApp:(NSString *)json { NSRange range = [json rangeOfString:@"BM_ANGEL:qrcode_scan_oppenapp#"]; NSMutableString *mstring = [NSMutableString stringWithString:json]; NSString *result = [json substringFromIndex:range.length]; NSString *appid = [self subAppid:result]; DFBLog(@"appid=======%@",appid); NSString *params = [self params:result]; NSString *correctedJSONString = [params stringByReplacingOccurrencesOfString:@"'" withString:@"\""]; NSDictionary *content = [correctedJSONString mj_JSONObject]; DFBLog(@"appid=======%@",appid); [self openApp:appid withName:@"" withUrl:@"" withParmas:content]; } - (NSString *)params:(NSString *)ppidString { // 找到params={...}的位置 NSRange paramsRange = [ppidString rangeOfString:@"params={"]; if (paramsRange.location != NSNotFound) { paramsRange.location += 7; // 跳过"params={" // 查找闭合的"}" NSRange closeBraceRange = [ppidString rangeOfString:@"}" options:NSLiteralSearch range:NSMakeRange(paramsRange.location, ppidString.length - paramsRange.location)]; if (closeBraceRange.location != NSNotFound) { // 截取params里面的内容 NSString *paramsContent = [ppidString substringWithRange:NSMakeRange(paramsRange.location, closeBraceRange.location - paramsRange.location + 1)]; return paramsContent; } return @""; } else { return @""; } } - (NSString *)subAppid:(NSString *)ppidString { NSRange ppidRange = [ppidString rangeOfString:@"appid="]; if (ppidRange.location != NSNotFound) { // 截取从 ppid= 之后的子字符串 NSString *ppidValueWithPrefix = [ppidString substringFromIndex:ppidRange.location + ppidRange.length]; // 查找 # 的位置(如果有) NSRange hashRange = [ppidValueWithPrefix rangeOfString:@"#"]; if (hashRange.location != NSNotFound) { // 截取到 # 之前的部分 NSString *ppidValue = [ppidValueWithPrefix substringToIndex:hashRange.location]; return ppidValue; } else { // 如果没有 #,则整个 ppidValueWithPrefix 就是我们要的值 return ppidValueWithPrefix; } } else { return @""; } } - (NSDictionary *)parseParamsString:(NSString *)paramsString { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; // 这里为了简单起见,我们假设 paramsString 是正确的,并且格式固定 // 在实际使用中,您可能需要使用更健壮的解析方法,比如 NSJSONSerialization 或者正则表达式 NSArray *components = [paramsString componentsSeparatedByString:@","]; for (int i = 0; i < components.count; i += 2) { NSString *key = [components[i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSString *value = [components[i+1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // 去除尾部的单引号 value = [value substringToIndex:value.length - 1]; dict[key] = value; } return dict; }
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值