如何将字符串转换为 IP 地址?
How to convert string into an IP Address?
我正在使用一个函数来添加 .在字符串中的每三个字符之后。但是我怎样才能去掉数字前面的“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?
您可以使用字符串函数并在 componentsSeparatedByString
和 componentsJoinedByString
函数的帮助下获得预期的输出。
同样使用下面的方法:
-(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
希望这对您有所帮助!
我的建议是:
NSString *ipAddress = @"010.000.001.016";
将字符串拆分为子字符串数组:
NSArray *ipAddressComponents = [myString componentsSeparatedByString:@"."];
运行 通过数组进行 for 循环并 将每个 转换为 int 表示形式:
for(int i = 0; i < ipAddressComponents.count;i++)
{
[ipAddressComponents objectAtIndex:i] = [NSString stringWithFormat:@"%d",[[ipAddressComponents objectAtIndex:i]intValue]];
}
将数组加入返回字符串
NSString *newIpAddress = [ipAddressComponents componentsJoinedByString:@"."];
这应该会得到一个 IP 地址:10.0.1.16
在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.
}
}
最后将创建的数组作为带点组件的字符串加入
我正在使用一个函数来添加 .在字符串中的每三个字符之后。但是我怎样才能去掉数字前面的“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?
您可以使用字符串函数并在 componentsSeparatedByString
和 componentsJoinedByString
函数的帮助下获得预期的输出。
同样使用下面的方法:
-(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
希望这对您有所帮助!
我的建议是:
NSString *ipAddress = @"010.000.001.016";
将字符串拆分为子字符串数组:
NSArray *ipAddressComponents = [myString componentsSeparatedByString:@"."];
运行 通过数组进行 for 循环并 将每个 转换为 int 表示形式:
for(int i = 0; i < ipAddressComponents.count;i++) { [ipAddressComponents objectAtIndex:i] = [NSString stringWithFormat:@"%d",[[ipAddressComponents objectAtIndex:i]intValue]]; }
将数组加入返回字符串
NSString *newIpAddress = [ipAddressComponents componentsJoinedByString:@"."];
这应该会得到一个 IP 地址:10.0.1.16
在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.
}
}
最后将创建的数组作为带点组件的字符串加入