Arrays 使用目标C将子字符串替换为字符串
我正在尝试用现有的URL IP地址替换我的自定义IP地址。如何用现有IP替换我的IP地址。请在下面找到代码片段Arrays 使用目标C将子字符串替换为字符串,arrays,string,replace,substring,Arrays,String,Replace,Substring,我正在尝试用现有的URL IP地址替换我的自定义IP地址。如何用现有IP替换我的IP地址。请在下面找到代码片段 - (IBAction)btnSubmit:(UIButton *)sender { NSString *ipAdd = [_txtIPAddress text]; NSLog(@"My entered IP Address is %@", ipAdd); NSLog(@"Current pointing server value
- (IBAction)btnSubmit:(UIButton *)sender {
NSString *ipAdd = [_txtIPAddress text];
NSLog(@"My entered IP Address is %@", ipAdd);
NSLog(@"Current pointing server value is %@", [ConnManager activeEndpoint]);
NSLog([App appDelegate].isConfigured ? @"Yes" : @"No");
NSArray *listItems = [[ConnManager activeEndpoint] componentsSeparatedByString:@"/"];
ConnManager *conn = [[ConnManager alloc] init];
NSMutableString *configuredUrl = nil;
[configuredUrl setString:@""];
for ( NSInteger i =0; i < listItems.count; i++) {
if(i != 2) {
NSMutableString * arrayElement = [(NSMutableString*)listItems objectAtIndex:i];
NSMutableString *str = [NSMutableString stringWithFormat: @"%@/", arrayElement];
[configuredUrl appendString:str];
} else {
NSMutableString *configstr = [NSMutableString stringWithFormat: @"%@", ipAdd];
NSLog(@"ConfigString %@", configstr);
//How to replace the my ip address with the array index[2]
configuredUrl = [configuredUrl stringByAppendingFormat:configstr];
NSLog(@"Configured Url after apppending %@", configuredUrl);
-(iAction)btnSubmit:(UIButton*)发送方{
NSString*ipAdd=[\u txtIPAddress text];
NSLog(@“我输入的IP地址为%@”,ipAdd);
NSLog(@“当前定点服务器值为%@”,[ConnManager activeEndpoint]);
NSLog([App appDelegate].isConfigured?@“是”:@“否”);
NSArray*listItems=[[ConnManager activeEndpoint]组件通过字符串分离:@/“];
ConnManager*conn=[[ConnManager alloc]init];
NSMutableString*configuredUrl=nil;
[configuredUrl设置字符串:@”“;
对于(NSInteger i=0;i
提前感谢试试这个较短的版本,它取代了行后的所有内容
NSLog([App appDelegate].isConfigured?@“Yes”:@“No”);
或者,如果activeEndpoint
是一个URL,则更容易
NSURL *url = [NSURL URLWithString:[ConnManager activeEndpoint]];
NSString *configuredURL = [[[NSURL alloc] initWithScheme:[url scheme] host:ipAdd path:[url path]] absoluteString];
将URL解析留给系统类通常更容易。如果可以,我建议使用
NSURLComponents
。例如:
NSURLComponents *URLComponents =
[NSURLComponents componentsWithString:[ConnManager activeEndpoint]];
URLComponents.host = [_txtIPAddress text];
NSString *configuredURL = URLComponents.string;
非常感谢。它可以工作,但我得到的URL是http:/xx.x.xxx.xx/xx/xx。如何在http之后再插入一个斜杠:
NSURLComponents *URLComponents =
[NSURLComponents componentsWithString:[ConnManager activeEndpoint]];
URLComponents.host = [_txtIPAddress text];
NSString *configuredURL = URLComponents.string;