- クラスメソッド
- + availableStringEncodings
- + defaultCStringencoding
- + localizedNameOfStringEncoding:
- + localizedStringWithFormat:
- + pathWithComponents:
- + string
- + stringWithCharacters:length:
- + stringWithContentsOfFile:encoding:error:
- +stringWithContentsOfFile:usedEncoding:error:
- + stringWithContentsOfURL:encoding:error:
- + stringWithContentsOfURL:usedEncoding:error:
- + stringWithCString:encoding:
- + stringWithFormat:
- + stringWithString:
- + stringWithUTF8String:
- Referred from
- Copyright
- NSStringクラスリファレンスへもどる
クラスメソッド
+ availableStringEncodings
使用しているアプリケーションの環境で、文字列オブジェクトがサポートするエンコーディングを、ゼロ終端のリストで返します。
を格納している、ゼロ終端のリストを返します。
+ (const NSStringencoding *)availableStringEncodings
Return Value
使用しているアプリケーションの環境で、文字列オブジェクトがサポートするエンコーディングの、ゼロ終端処理されたリスト。
アプリケーションの環境で文字列オブジェクトが対応するエンコーディングを格納している、ゼロ終端のリスト。
Discussion
より一般的に使われるエンコーディングは次の通り:
NSASCIIStringEncoding
NSUnicodeStringEncoding
NSISOLatin1StringEncoding
NSISOLatin2StringEncoding
NSSymbolStringEncoding
サポートしている多数のエンコーディングのリストと解説は、"Constants"セクションを参照して下さい。そこでリストに上がっているエンコーディングに加えて、Core FoundateionでCFStingと定義されているエンコーディングも使えます。それらエンコーディングを使いやすいフォーマットへ変換するには、CFStringConvertEncodingToNSStringEncoding関数を呼ぶ必要があるだけです。
Availability
Available in OS X v10.0 and later.
See Also
+ localizedNameOfStringEncoding:
Declared In
NSString.h
+ defaultCStringencoding
どんなメソッドもC文字列を引数として受け入れると思われるC文字列のエンコーディングを返します。
+ (NSStringEncoding)defaultCStringencoding
Return Value
どんなメソッドもC文字列を引数として受け入れると思われるC文字列のエンコーディング
Discussion
このメソッドは、ユーザーのデフォルト言語とほかの因子からの類推から得られるユーザ依存のエンコーディングを返します。エンコーディングが分からない文章を解釈するとき、その他にヒントがないなら、このエンコーディングを使う必要が時にあるかもしれません。しかし、一般的にはこのエンコーディングはほとんど、もしかしたらまったく使われることはありません。エンコーディングを類推できそうな値から、それがまったく単純なCOLOR(#888888({NSStrng}あったとしても、期待していなかったエンコーディング変換が結果となりうるのに注意して下さい。たとえば、双方向性のエンコードを持つ句読点の文字を変換する際に起こったりします。
C文字列を引数として受け入れるメソッドは、COLOR(#888888(...CString...}というキーワードを持っています。たとえばstringWithCStringメソッドです。そうではありますが、それらは廃止されているのを注意してください。デフォルトのC文字列はシステム情報から決定し、個々のプロセスにおいてプログラムから変更はできません。サポートしているエンコーディングが全てリストしてある“String Encodings”を参照してください。
Availability
Available in OS X v10.0 and later.
Related Sample Code
TextEdit
Declared In
NSString.h
+ localizedNameOfStringEncoding:
あたえられたエンコーディングの名前を示す、可読性のある文字列を返します。
+ (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding
Parameters
encoding
文字列エンコーディング
Return Value
使用しているロケールの言語でのencoding名を示す、可読性のある文字列
Availability
Available in OS X v10.0 and later.
Related Sample Code
NSFontAttributeExplorer
TextEdit
Declared In
NSString.h
+ localizedStringWithFormat:
あたえられたフォーマット文字列をテンプレートとして使い、ユーザデフォルトのロケールに従って残りの引数で置き換えて文字列を生成して返します。
+ (instancetype)localizedStringWithFormat:(NSString *)format, ...
Parameters
format
フォーマット文字列。どのようにこのメソッドを使うかは“Formatting String Objects”を、フォーマット指定子の全てのリストは“String Format Specifiers”を、それぞれ参照してください。この値はnilではいけません。
formatがnilなら、NSInvalidArgumentExceptionが発生します。
...
formatへ置き換える、引数のコンマ区切りリスト。
Return Value
formatをテンプレートとして使い、ユーザデフォルトのロケールに従って残りの引数で置き換えて文字列。
Discussion
このメソッドはinitWithFormat:locale:を使うのと等価で、ロケール引数としてシステムロケールを渡しています。
フォーマット文字列の例としては、このメソッドはロケールに従った数字で%fや%dを置き換え、必要ならばdescriptionに代わりdescriptionWithLocale:を呼び出します。
次の抜粋コードは文字列とfloatから新しい文字列を生成します:
NSString *myString = [NSString localizedStringWithFormat:@"%@: %f\n", @"Cost", 1234.56];}
ロケールがen_USなら得られる文字列は“Cost: 1234.560000\n”で、fr_FRなら“Cost: 1234,560000\n”となります。
さらに多くの情報は“Formatting String Objects”を参照してください。
Availability
Available in OS X v10.0 and later.
See Also
+ stringWithFormat:
- initWithFormat:locale:
Related Sample Code
Sprite Tour
Target-Action using Cocoa Bindings
Declared In
NSString.h
+ pathWithComponents:
あたえられた配列にある文字列を、パス区切り文字でお互いにつなげて組み上げた文字列を返します。
+ (NSString *)pathWithComponents:(NSArray *)components
Parameters
components
ファイルパスを表すNSStingオブジェクトを格納した配列。絶対パスを生成するには、最初の要素へスラッシュ(“/”)を使います。最後にパス分割子(つまりスラッシュ)を含めるには、最後の要素に空の文字列を使います。
Return Value
文字列を(あらわれている順に)パス分割子をはさんでつなげて組み上げた文字列。
配列の要素である文字列を、パス分割子ではさみつなげて組み上げた文字列。
Discussion
このメソッドは生成したパスから意味をなさない部分を除いたりしません。空の要素や親ディレクトリへの参照といったのを解決するにはstringByStandardizingPathを使ってください。
Availability
Available in OS X v10.0 and later.
See Also
– pathComponents
Related Sample Code
From A View to A Movie
From A View to A Picture
Declared In
NSPathUtilities.h
+ string
空の文字列を返します。
+ (instancetype)string
Return Value
空の文字列。
Availability
Available in OS X v10.0 and later.
See Also
– init
Related Sample Code
AppSandboxLoginItemXPCDemo
Drawing Along a Path Using Core Text with Cocoa
iSpend
UIElementInspector
Unit Testing Apps and Frameworks
Declared In
NSString.h
+ stringWithCharacters:length:
あたえられたUnicode文字列のC配列から、あたえられた数だけの文字を含む文字列を返します。
+ (instancetype)stringWithCharacters:(const unichar *)chars length:(NSUInteger)length
Parameters
chars
Unicode文字のC配列。この値はNULLではいけません。
重要:文字がNULL、長さが0でもあっても、例外が発生します。
length
charから使う文字の数。
Return Value
1文字目から始まりcharから取り出される長さlength'のUnicode文字を含む文字列。
Availability
Available in OS X v10.0 and later.
See Also
– initWithCharacters:length:
Declared In
NSString.h
+ stringWithContentsOfFile:encoding:error:
あたえられたパスにあるファイルからデータを読み込み、あたえられたエンコーディングで解釈して作成した文字列を返します。
+ (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
Parameters
path
ファイルへのパス。
enc
pathにあるファイルのエンコーディング。
error
エラーが発生したら、問題を記述するNSErrorが含まれています。発生の可能性があるエラーを無視したいなら、NULLを渡してください。
Return Value
pathの名前が付いたファイルからデータを読み込み、あたえられたエンコーディングencで解釈して作成した文字列。ファイルを開けられなかったり、エンコーディングの際にでエラーが発生したら、nilを返します。
Availability
Available in OS X v10.4 and later.
See Also
– initWithContentsOfFile:encoding:error:
Related Sample Code
AVFoundation - Timecode Reader/Writer (avtimecodereadwrite)
CIAnNOtation
CIDemoImageUnit
JavaScriptCoreHeadstart
QTCoreVideo301
Declared In
NSString.h
+stringWithContentsOfFile:usedEncoding:error:
あたえられパスにあるファイルからデータを読み込んで生成した文字列を返します。一方で、ファイルを解釈するのに使用したエンコーディングはリファレンス渡しで返します。
+ (instancetype)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringencoding *)enc error:(NSError **)error
Parameters
path
ファイルへのパス。
enc
ファイルの読み込みが成功したら、pathにあるファイルを解釈するのに使われたエンコーディングを含みます。これは参照渡しで返されます。
error
エラーが発生すると、問題について記述したNSErrorオブジェクトが含まれています。発生の可能性があるエラーに興味がなければ、NULLを渡すといいでしょう。
Return Value
pathの名前を持つファイルからデータを読み込み、それから生成した文字列。ファイルが読み込めないかエンコーディングでエラーが発生したら、nilを返します。
Discussion
このメソッドは、pathにあるファイルのエンコーディングを決定しようと試みます。
Availability
Available in OS X v10.4 and later.
See Also
- initWithContentsOfFile:encoding:error:
Declared In
NSString.h
+ stringWithContentsOfURL:encoding:error:
あたえられたURLにあるデータを読み込み、あたえられたエンコーディングを用いて解釈して生成した文字列を返します。
+ (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
Parameters
url
読み込むURL。
enc
urlにあるデータのエンコーディング。
error
エラーが発生したら、問題を記述したNSErrorオブジェクトが含まれます。発生の可能性があるエラーに興味がなければ、NULLを渡してください。
Return Value
urlからデータを読み込み、endエンコーディングを用いて解釈して生成した文字列。もしURLを開けないかエンコードエラーが発生したら、nilが返されます。
Availability
Available in OS X v10.4 and later.
See Also
+ stringWithContentsOfURL:usedEncoding:error:
- initWithContentsOfURL:encoding:error:
Related Sample Code
CIHazeFilterSample
CryptoCompatibility
Writing Subtitles to a Movie from the Command Line for OS X
Declared In
NSString.h
+ stringWithContentsOfURL:usedEncoding:error:
あたえられたURLからデータを読み込み、それから生成した文字列を返します。データを解釈するのに使われたエンコーディングを参照渡しで返します。
+ (instancetype)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringencoding *)enc error:(NSError **)error
Parameters
url
データを読み込むURL
enc
urlに成功したら、encはデータを解釈するのに使われたエンコードを含みます。
error
エラーが発生したら、問題を記述したNSErrorオブジェクトが含まれています。発生の可能性があるエラーに興味がなければ、NULLを渡してください。
Return Value
urlから読み込んだデータで作成した文字列。もしURLが開けない、またはエンコーディングエラーが発生したら、nilが返されます。
Discussion
このメソッドは、urlのエンコーディングを決定しようと試みます。
Availability
Available in OS X v10.4 and later.
See Also
+ stringWithContentsOfURL:encoding:error:
- initWithContentsOfURL:usedEncoding:error:
Declared In
NSString.h
+ stringWithCString:encoding:
あたえられたC配列にあるバイト列を、あたえられたエンコーディングに従って解釈した文字列を返します。
+ (instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
Parameters
cString
バイト列のC配列。配列はNULL}で終わらなければならず、途中のNULLを許可しません。
enc
cStringのエンコーディング。
Return Value
cStringで記述されている文字を含む文字列。
Discussion
もしcStringがNULL終端のC文字列でないなら、または'encが実際のエンコーディングと同じでなかったら、得られる結果は定義されていません。
Special Considerations
NULLバイトは許可されていないので、16ビットエンコーディングは一般的にNULLバイトを含むことからサポートされていません。
Availability
Available in OS X v10.4 and later.
See Also
– initWithCString:encoding:
Related Sample Code
AVCustomEditOSX
avmetadataeditor
SMARTQuery
Supporting Multiple GPUs
Writing Subtitles to a Movie from the Command Line for OS X
Declared In
NSString.h
+ stringWithFormat:
あたえられたフォーマット文字列をテンプレートとして、続いてあたえられる引数の値で置き換えを行って生成した文字列を返します。
+ (instancetype)stringWithFormat:(NSString *)format,, ...
Parameters
format
フォーマット文字列。このメソッドをどうやって使うかは“Formatting String Objects”を、フォーマット指定子のリストは“String Format Specifiers”をそれぞれを参照してください。この値はnilではいけません。
重要:フォーマットがnilなら、NSInvalidArgumentExceptionが発生します。
...
formatへ置き換えする、コンマで区切られたリスト。
Return Value
テンプレートとしてあたえられたフォーマットを使い、これに続く引数を標準ロケールに合わせて置き換えを行って生成した文字列。
Discussion
このメソッドはlocalizedStringWithFormat:に似ていますが、数をフォーマットするのに標準ロケールを使います。これは、たとえば、ファイルへ書き込んだあとにパースし直すのに必要な"NOn-localized"フォーマットを作りたいとき便利です。
Availability
Available in OS X v10.0 and later.
See Also
- initWithFormat:
+ localizedStringWithFormat:
Related Sample Code
From A View to A Picture
NSFontAttributeExplorer
OpenALExample
UIElementInspector
VirtualScanner
Declared In
NSString.h
+ stringWithString:
あたえられた文字列から文字をコピーして生成した文字列を返します。
+ (instancetype)stringWithString:(NSString *)aString
Parameters
aString
文字をコピーしてくるための文字列。nilではいけません。
重要:aStringがnilなら、NSInvalidArgumentExceptionが発生します。
Return Value
aStringから文字をコピーして生成した文字列。
Availability
Available in OS X v10.0 and later.
See Also
– initWithString:
Related Sample Code
CoreWLANWirelessManager
From A View to A Movie
From A View to A Picture
NSOperationSample
Using NSTokenField
Declared In
NSString.h
+ stringWithUTF8String:
あたえられたUTF-8バイト列のC配列のデータをコピーして生成した文字列を返します。
+ (instancetype)stringWithUTF8String:(const char *)bytes
Parameters
bytes
UTF8エンコーディングの、NULLを終端とするC文字列。(訳者注:"UTF8"原文のまま、"UTF-8"でしょう)
重要:もしbytesがNULLなら、例外が発生します。
Return Value
bytesからデータをコピーして生成した文字列。
Availability
Available in OS X v10.0 and later.
See Also
- initWithString:
Related Sample Code
avexporter
CFHostSample
DNSSDObjects
OpenALExample
OpenCL NBody Simulation Example
Declared In
NSString.h
Referred from
NSString Class Reference, Apple Developer Library
Copyright
Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-09-18