-1

I have an NSString that is a local IP Address, and I need to remove the last set of digits on this IP Address and replace with 255. I am doing this to create broadcast packet from the local IP Address.

I have checked all the available string manipulation functions and cannot find a Left function, which would help for this situation.

Basically I want to do this:-

Convert 192.168.1.10 to 192.168.1.255

Edit: Please note the following information for calculating the broadcast address for a network. It is calculated by performing a bitwise OR with the IP Address and the inverted Netmask. You can see a calculator here - http://www.subnet-calculator.com/

Also the actual code on how to calculate the broadcast address is available here - Calculating the Broadcast Address in Objective-C

This was added to avoid any confusion created with my question.

Community
  • 1
  • 1
Remixed123
  • 1,575
  • 4
  • 21
  • 35
  • Wow! Some of you guys are tough (or perhaps just mean spirited). I cannot believe I got a down vote for this question. I did my research, this was not available in any previous post or on the web. And look at the answer, it was not so simple! This question is sure to help others in the future. And check out this far simplier question of the same types, it has 79 upvotes - http://stackoverflow.com/questions/1082178/objective-c-remove-last-character-from-string?lq=1 – Remixed123 Jan 11 '14 at 17:53

4 Answers4

2

Getting index of a character in NSString with offset & using substring in Objective-C shows you how to find the range of a string within another string, including option of a backwards search. Using that, find the range of @"." (with the NSBackwardsSearch option) within your IP address ipaddr.

Assuming you got the range into a variable named range, range.location is what you need but range.length is not (it's 1, the length of @".") so fix that: range.length = [ipaddr.length] - range.location;.

Finally, use that range to perform a replacement: maskAddress = [ipaddress stringByReplacingCharactersInRange:range withString:@".255"];

Now maskAddr contains the replacement you've described.

Community
  • 1
  • 1
mah
  • 39,056
  • 9
  • 76
  • 93
  • I just read your profile. So apologies, I know you put a lot of effort into your answer. But in the end, as a coder who learns from looking at, and reviewing code, nothing beats code. We all learn differently, and I've been programming for over 30 years, so conceptually I am unlikely to learn something new, but syntax and the approaches of different languages, well that is another things. I did provide a up vote :-) – Remixed123 Jan 11 '14 at 18:07
2
NSScanner* scanner = [NSScanner scannerWithString:idAddress];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"."]];
int a, b, c;
[scanner scanInt:&a];
[scanner scanInt:&b];
[scanner scanInt:&c];
NSString* result = [NSString stringWithFormat:@"%d.%d.%d.255", a, b, c];
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22
  • 1
    But replacing last number with 255 work only for network mask '255.255.255.0'. Normaly you need use bitwise OR IP with inverted Netmask to get boarcast IP. – Cy-4AH 11 mins ago – Cy-4AH Jan 11 '14 at 15:06
  • Not something I have taken into consideration or tested. So thanks, I will look into this! – Remixed123 Jan 11 '14 at 17:54
1

NSString has -componentsSeparatedByString: which you could use to separate the string into an array (if you specify @“.” as the string), change the array, then put them back together with NSArray’s -componentsJoinedByString:

Or you could use NSString’s -rangeOfString:@“.” options:NSBackwardsSearch to find where the last “.” is, and then pull apart the string with substringToIndex: and append your @“255”.

Or you could use NSString’s -stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange and specify that you’re doing a RegEx, but that’d be harder.

Wil Shipley
  • 9,343
  • 35
  • 59
0

you can use a method substringToIndex on NSSring object like they did here

and then append a string or use NSStringWithFormat to create a new one

Community
  • 1
  • 1
Julian
  • 9,299
  • 5
  • 48
  • 65