Objective-C 中的字符串操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1778227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:32:34  来源:igfitidea点击:

String Manipulation in Objective-C

objective-cstring

提问by Kaji

Gotten a hold on how to fetch and write to variables in Objective-C, now it's time to learn how to do something more useful with them! Right now, I'm primarily trying to figure out how string manipulation works. In particular, I'm looking for the following functions:

掌握了如何在 Objective-C 中获取和写入变量,现在是时候学习如何用它们做一些更有用的事情了!现在,我主要是想弄清楚字符串操作是如何工作的。特别是,我正在寻找以下功能:

  • Concatenation
  • Finding the length of a string (especially multi-byte/UTF-8 strings; I do a lot of work with East Asian languages)
  • Pulling just a portion of a string (e.g. the "foobar" out of "abcfoobarxyz")
  • Searching within a string (see the above example)
  • Changing case (upper, lower, title if it's simple to do)
  • Exploding/Imploding strings (e.g. creating and getting information from comma-separated lists)
  • Find/Replace within strings
  • Any other generally useful string functions that might be available
  • 级联
  • 查找字符串的长度(尤其是多字节/UTF-8 字符串;我用东亚语言做了很多工作)
  • 仅拉出字符串的一部分(例如“abcfoobarxyz”中的“foobar”)
  • 在字符串中搜索(见上面的例子)
  • 更改大小写(上、下、标题,如果很简单的话)
  • 爆炸/内爆字符串(例如,从逗号分隔的列表中创建和获取信息)
  • 在字符串中查找/替换
  • 可能可用的任何其他通常有用的字符串函数

回答by Alexandre Cassagne

Examples: Concatenation:

示例: 串联:

- (NSString*) concatenateString:(NSString*)stringA withString:(NSString*)stringB
{  
    NSString *finalString = [NSString stringWithFormat:@"%@%@", stringA,
                                                       stringB];
    return finalString;
}
// The advantage of this method is that it is simple to put text between the
// two strings (e.g. Put a "-" replace %@%@ by %@ - %@ and that will put a
// dash between stringA and stringB

String Length:

字符串长度:

- (int) stringLength:(NSString*)string
{
    return [string length];
    //Not sure for east-asian languages, but works fine usually
}

Remove text from string:

从字符串中删除文本:

- (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input
{
   return [input stringByReplacingOccurrencesOfString:textToRemove
                                           withString:@""];
}

Uppercase / Lowercase / Titlecase:

大写/小写/标题:

- (NSString*)uppercase:(NSString*)stringToUppercase
{
   return [stringToUppercase uppercaseString];
}

- (NSString*)lowercase:(NSString*)stringToLowercase
{
   return [stringToLowercase lowercaseString];
}

- (NSString*)titlecase:(NSString*)stringToTitleCase
{
   return [stringToTitleCase capitalizedString];
}

Find/Replace

查找/替换

- (NSString*)findInString:(NSString*)string
        replaceWithString:(NSString*)stringToReplaceWith
{
   return [input stringByReplacingOccurrencesOfString:string
                                           withString:stringToReplaceWith];
}

I hope this helps!

我希望这有帮助!

PS: Don't forget to check the documentation, and Google is your friend. Good luck

PS:不要忘记查看文档,Google 是您的朋友。祝你好运

回答by f3lix

There is a String Programming Guide for Cocoawhose table of contents comes awfully close to your list of questions.

有一个Cocoa 字符串编程指南,其目录非常接近您的问题列表。

Besides this, you might want to look at the documentation of NSString(& NSMutableString) and NSScanner.

除此之外,您可能想查看NSString(& NSMutableString) 和NSScanner 的文档

回答by rein

In Xcode, press CMD-SHIFT-D and search for NSString.h, NSMutableString and/or NSScanner

在 Xcode 中,按 CMD-SHIFT-D 并搜索 NSString.h、NSMutableString 和/或 NSScanner

Open those files and look at all the things you can do with NSStrings. This should tell you what functions there are.

打开这些文件,看看你可以用 NSStrings 做的所有事情。这应该会告诉您有哪些功能。

By the way, this should become second nature to you as you'll be using this a lot to find out what the functions and/or delegates are.

顺便说一句,这应该成为您的第二天性,因为您将经常使用它来找出函数和/或委托是什么。

回答by mpemburn

Here's a "title case" routine for you:

这是您的“标题案例”例程:

- (NSString *) stringToTitleCase: (NSString *) inString
{
    NSString *firstLetter = [inString substringWithRange: NSMakeRange(0, 1)];
    NSString *theRest = [inString substringWithRange: NSMakeRange(1, [inString length]-1)];

    return [NSString stringWithFormat: @"%@%@", [firstLetter uppercaseString], theRest];
}

回答by KarthicK

- (NSString *) stringToTitleCase: (NSString *) inString
{
    NSString *firstLetter = [inString substringWithRange: NSMakeRange(0, 1)];
    NSString *theRest = [inString substringWithRange: NSMakeRange(1, [inString length]-1)];

    return [NSString stringWithFormat: @"%@%@", [firstLetter uppercaseString], theRest];
}

回答by Ken

Those are all written up very clearly in the documentation.

这些都在文档中写得很清楚。