Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用目标C将子字符串替换为字符串_Arrays_String_Replace_Substring - Fatal编程技术网

Arrays 使用目标C将子字符串替换为字符串

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

我正在尝试用现有的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 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;