设置“你好,世界”中的文本格式。你好吗?到“你好,世界。你好吗?在 ObjC(iOS) 中?

发布于 2024-11-25 15:59:45 字数 113 浏览 5 评论 0原文

正如标题所说,我需要格式化一串文本,格式如下:“HELLO,WORLD。HOW ARE YOU?”进入“你好,世界。你好吗?”,在iOS中有没有标准的方法可以做到这一点?或者有示例代码吗?

谢谢!

As the title said, I need to format a string of text in format like: "HELLO, WORLD. HOW ARE YOU?" into "Hello, world. How are you?", is there any standard method for doing this in iOS? Or is there any sample code ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神魇的王 2024-12-02 15:59:45

我不知道执行此操作的标准方法,而且我认为没有标准方法。
但是NSString提供了这样的方法:-(NSString *)capitalizedString;它返回一个新字符串,每个单词的第一个字符在上案件。正确折断绳子后,您就可以使用它了。还可以考虑使用以下方式获取小写字符串:-(NSString *)lowercaseString

I don't know standard method to do this, and I don't think there is one.
But the NSString provides methods such : -(NSString *)capitalizedString; which returns a new string, with the first character of each word in upper case. After breaking correctly your string you could use it. Also think of getting a string in lower case using : -(NSString *)lowercaseString.

你没皮卡萌 2024-12-02 15:59:45

没有直接的方法可以将段落中句子的第一个字符大写。 NSString 只有一个方法capitalizedString,它可以将每个单词大写。您必须创建自己的逻辑才能实现这一目标。您可以尝试以下代码。

- (NSString *)captilizeParagraph:(NSString *)paragraph {

    if ([paragraph length] == 0) return paragraph;
    NSArray *sentences = [paragraph componentsSeparatedByString:@". "];
    NSMutableArray *capitalizedSentences = [NSMutableArray array];

    for (NSString *sentence in sentences) {

        sentence = [sentence lowercaseString];
        sentence = [sentence stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[sentence substringToIndex:1] uppercaseString]];
        [capitalizedSentences addObject:sentence];
    }

    NSString *capitalizedParagrah = [capitalizedSentences componentsJoinedByString:@". "];
    return capitalizedParagrah;
}

注意:上面的代码假设段落中的所有句子都以字符 ". "(一个点和一个空格)结尾,除了最后一个句子(它可以以任何字符结尾)特点)。如果句子以 "? ""! " 等其他字符结尾,则此方法将返回不完全格式化的字符串。

There is no direct way to capitalize the first characters of sentences in a paragraph. NSString just has a method capitalizedString which capitalizes each and every word. You have to create your own logic to achieve this. You can try the following code.

- (NSString *)captilizeParagraph:(NSString *)paragraph {

    if ([paragraph length] == 0) return paragraph;
    NSArray *sentences = [paragraph componentsSeparatedByString:@". "];
    NSMutableArray *capitalizedSentences = [NSMutableArray array];

    for (NSString *sentence in sentences) {

        sentence = [sentence lowercaseString];
        sentence = [sentence stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[sentence substringToIndex:1] uppercaseString]];
        [capitalizedSentences addObject:sentence];
    }

    NSString *capitalizedParagrah = [capitalizedSentences componentsJoinedByString:@". "];
    return capitalizedParagrah;
}

Note: The above code assumes that all the sentences in the paragraph ends with characters ". " (a dot and a space) except the last sentence(it can end with any character). If a sentence ends with some other characters like "? " or "! ", then this method will return a not-fully-formatted string.

尬尬 2024-12-02 15:59:45
-(NSString *)normalizeString{
    __block NSString *string = [self lowercaseString];
    string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[string substringToIndex:1] uppercaseString]];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\p{P}-[,]]" options:0 error:nil];
    [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        if(result.range.location + 2 < string.length) string = [string stringByReplacingCharactersInRange:NSMakeRange(result.range.location + 2, 1) withString:[[self substringWithRange:NSMakeRange(result.range.location + 2, 1)] uppercaseString]];
    }];

    return string;
}

假设标点符号后的 2 个字符是下一个句子的第一个字符,则此方法有效。

-(NSString *)normalizeString{
    __block NSString *string = [self lowercaseString];
    string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[string substringToIndex:1] uppercaseString]];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\p{P}-[,]]" options:0 error:nil];
    [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        if(result.range.location + 2 < string.length) string = [string stringByReplacingCharactersInRange:NSMakeRange(result.range.location + 2, 1) withString:[[self substringWithRange:NSMakeRange(result.range.location + 2, 1)] uppercaseString]];
    }];

    return string;
}

Assuming that 2 characters after the punctuation is the first character of the next sentence, this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文