【问题标题】:How to convert string into an IP Address?如何将字符串转换为IP地址?
【发布时间】:2017-06-08 12:35:42
【问题描述】:

我正在使用一个函数来添加 .在字符串中每三个字符之后。但是如何删除数字前面的“0”。

-(NSString *)formatStringAsIpAddress:(NSString*)MacAddressWithoutColon
{
    NSMutableString *macAddressWithColon = [NSMutableString new];
    for (NSUInteger i = 0; i < [MacAddressWithoutColon length]; i++)
    {
        if (i > 0 && i % 3 == 0)
            [macAddressWithColon appendString:@"."];
        unichar c = [MacAddressWithoutColon characterAtIndex:i];
        [macAddressWithColon appendString:[[NSString alloc] initWithCharacters:&c length:1]];
    }
    return macAddressWithColon;
}

如果我有 IP 地址 010.000.001.016 我怎样才能像 10.0.1.16 一样设置它?如果我有 IP 地址 192.168.001.001?如何从IP地址中删除前0?

【问题讨论】:

  • 不是更漂亮的方式,但是:NSMutableArray *numberComponents = [[NSMutableArray alloc] init]; for (NSString *aSubString in [str componentsSeparatedByString:@"."]) { [numberComponents addObject:@([aSubString integerValue])]; } NSString *final = [numberComponents componentsJoinedByString:@"."]; 可以解决问题
  • 不就是把整个字符串地址用句点分割成一个Int数组吗?
  • 正如@El Tomato 所说 - 将字符串拆分为三个字符的块,将它们转换为 Int,并从中创建没有前导零的字符串

标签: ios objective-c nsstring


【解决方案1】:

我的建议:

    NSString *ipAddress = @"010.000.001.016";
  1. 字符串拆分为子字符串数组:

    NSArray *ipAddressComponents = [myString componentsSeparatedByString:@"."];
    
  2. 使用 for 循环遍历数组并将每个数组convert 转换为 int 表示:

    for(int i = 0; i < ipAddressComponents.count;i++)
    {
        [ipAddressComponents objectAtIndex:i] = [NSString stringWithFormat:@"%d",[[ipAddressComponents objectAtIndex:i]intValue]];
    }
    
  3. 加入数组回到字符串

    NSString *newIpAddress = [ipAddressComponents componentsJoinedByString:@"."];
    

这应该会导致获得一个 IP 地址:10.0.1.16

【讨论】:

    【解决方案2】:

    您可以使用字符串函数并借助 componentsSeparatedByStringcomponentsJoinedByString 函数获得预期的输出。

    同样使用以下方法:

    -(NSString*)removeZeroPrefix:(NSString*)aStrIP{
    
        NSMutableArray *subStringArray = [[NSMutableArray alloc] init];
    
        NSArray *arrayOfSubString = [aStrIP componentsSeparatedByString:@"."];
    
        for (NSString *aSingleString in arrayOfSubString)
        {
            [subStringArray addObject:[NSNumber numberWithInteger:[aSingleString integerValue]]];
        }
    
        return [subStringArray componentsJoinedByString:@"."];
    
    }
    

    用法:

    [self removeZeroPrefix:@"010.000.001.016"];
    [self removeZeroPrefix:@"192.168.001.001"];
    

    输出:

    Printing description of aStrIP:
    10.0.1.16
    Printing description of aStrIP:
    192.168.1.1
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      在 Swift 中

      let ip = "0125.052.000.10"
              let split = ip.components(separatedBy: ".")
              for number in split {
                  if let isNumber = Int(number){
                      print(isNumber) // add this isNumber into array which is shows without initial zeros.
                  }
              }
      

      最后将创建的数组作为带点组件的字符串加入

      【讨论】:

        猜你喜欢
        • 2021-10-15
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 1970-01-01
        • 2012-07-14
        • 2017-08-25
        • 2019-05-13
        • 2022-01-13
        相关资源
        最近更新 更多