林昊

Generation

testsWed, 19 Apr 2023

+ (NSString *)cutSting:(NSString *)string WithCharacterLength:(NSInteger)length { float sum = 0; NSInteger index = 0; for (int i = 0; i < string.length; i++) { NSString *character = [string substringWithRange:NSMakeRange(i, 1)]; if ([character lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3) { sum += 1; } else { sum += 0.5; } if (sum >= length) { index = i; break; } } if (index == 0) { return string; } else { NSString *subString = [string substringWithRange:NSMakeRange(0, index+1)]; return [NSString stringWithFormat:@"%@...", subString]; } }

## Authors * **huhuanming** - [Github](https://github.com/huhuanming) ## License This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

Questions about programming?Chat with your personal AI assistant