Note that there are some explanatory texts on larger screens.

plurals
  1. POTruncate delimited NSString without removing delimiters
    primarykey
    data
    text
    <p>I have some data in an NSString, separated by colons:</p> <pre><code>@"John:Doe:1970:Male:Dodge:Durango" </code></pre> <p>I need to limit the total length of this string to 100 characters. But I also need to ensure the correct number of colons are present.</p> <p>What would be a reasonable to way to truncate the string but also add the extra colons so I can parse it into the correct number of fields on the other side?</p> <p>For example, if my limit was 18, you would end up with something like this:</p> <pre><code>@"John:Doe:1970:Ma::" </code></pre> <p>Here's an updated version of my own latest pass at this. Uses @blinkenlights algorithm:</p> <pre><code>+ (NSUInteger)occurrencesOfSubstring:(NSString *)substring inString:(NSString *)string { // http://stackoverflow.com/a/5310084/878969 return [string length] - [[string stringByReplacingOccurrencesOfString:substring withString:@""] length] / [substring length]; } + (NSString *)truncateString:(NSString *)string toLength:(NSUInteger)length butKeepDelmiter:(NSString *)delimiter { if (string.length &lt;= length) return string; NSAssert(delimiter.length == 1, @"Expected delimiter to be a string containing a single character"); int numDelimitersInOriginal = [[self class] occurrencesOfSubstring:delimiter inString:string]; NSMutableString *truncatedString = [[string substringToIndex:length] mutableCopy]; int numDelimitersInTruncated = [[self class] occurrencesOfSubstring:delimiter inString:truncatedString]; int numDelimitersToAdd = numDelimitersInOriginal - numDelimitersInTruncated; int index = length - 1; while (numDelimitersToAdd &gt; 0) { // edge case not handled here NSRange nextRange = NSMakeRange(index, 1); index -= 1; NSString *rangeSubstring = [truncatedString substringWithRange:nextRange]; if ([rangeSubstring isEqualToString:delimiter]) continue; [truncatedString replaceCharactersInRange:nextRange withString:delimiter]; numDelimitersToAdd -= 1; } return truncatedString; } </code></pre> <p>Note that I don't think this solution handles the edge case from CRD where the number of delimiters is less than the limit.</p> <p>The reason I need the correct number of colons is the code on the server will split on colon and expect to get 5 strings back.</p> <p>You can assume the components of the colon separated string do not themselves contain colons.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload