Difference between revisions of "NSString"

From GNUstepWiki
Jump to navigation Jump to search
m
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
  
 
== Constructing Strings ==
 
== Constructing Strings ==
 +
 +
=== Constant Strings ===
  
 
As you can create static instances of char* plain C, you can create strings in GNUstep that automatically flatten out to be static instances of NSString. This is done by preceding the quoted string by an "at" symbol ('@'). For example:
 
As you can create static instances of char* plain C, you can create strings in GNUstep that automatically flatten out to be static instances of NSString. This is done by preceding the quoted string by an "at" symbol ('@'). For example:
Line 13: Line 15:
 
myString is now an NSString object, and can have methods called on it like any other string.
 
myString is now an NSString object, and can have methods called on it like any other string.
  
----
+
=== Copy Strings ===
  
 
You can also make a copy of a string as shown in the following example:
 
You can also make a copy of a string as shown in the following example:
Line 20: Line 22:
 
   NSString *newString = [NSString stringWithString: myString];
 
   NSString *newString = [NSString stringWithString: myString];
  
----
+
=== C Strings ===
  
 
Or if you already have a C string you can convert it to a NSString object as follows:
 
Or if you already have a C string you can convert it to a NSString object as follows:
Line 32: Line 34:
 
Note that using [NSString stringWithCString] is deprecated!
 
Note that using [NSString stringWithCString] is deprecated!
  
----
+
=== Formated Strings ===
  
 
A more flexable way of creating a string, using C style format flags follows:
 
A more flexable way of creating a string, using C style format flags follows:
Line 78: Line 80:
 
An array of string can also be joined together with the [array componentsJoinedByString:] method as follows:
 
An array of string can also be joined together with the [array componentsJoinedByString:] method as follows:
  
   NSArray *strings = [NSArray arrayWithObjects: @"John", @"Bob", @"Jane"];
+
   NSArray *strings = [NSArray arrayWithObjects: @"John", @"Bob", @"Jane", nil];
 
   NSString *string = [strings componentsJoinedByString: @", "];
 
   NSString *string = [strings componentsJoinedByString: @", "];
 
   // creating the string @"John, Bob, Jane"
 
   // creating the string @"John, Bob, Jane"
Line 96: Line 98:
 
   // Get the first six characters in the string ("John, ")
 
   // Get the first six characters in the string ("John, ")
 
    
 
    
  NSRange range = {3, 6};
+
   NSLog([string substringWithRange: NSMakeRange(3, 6)]);
   NSLog([string substringWithRange: range]);
 
 
   // Starting at index three get a string of length six ("hn, Bo")
 
   // Starting at index three get a string of length six ("hn, Bo")
  

Latest revision as of 04:51, 12 May 2009

Strings in GNUstep use the NSString class in gnustep-base. NSString is a immutable class, meaning it cannot change. For mutable strings, see NSMutableString. See the reference manual for gnustep-base for more details about NSString.

Constructing Strings

Constant Strings

As you can create static instances of char* plain C, you can create strings in GNUstep that automatically flatten out to be static instances of NSString. This is done by preceding the quoted string by an "at" symbol ('@'). For example:

 NSString * myString = @"This is an example of a string.";
 NSLog(myString);

myString is now an NSString object, and can have methods called on it like any other string.

Copy Strings

You can also make a copy of a string as shown in the following example:

 NSString *myString = @"This is an example of a string.";
 NSString *newString = [NSString stringWithString: myString];

C Strings

Or if you already have a C string you can convert it to a NSString object as follows:

 char *myString = "This is a C string.";
 ...
 NSString *NSString newString1 = [NSString stringWithCString: myString encoding: [NSString defaultCStringEncoding]];
 // Or:
 NSString *NSString newString2 = [NSString stringWithUTF8String: myString];

Note that using [NSString stringWithCString] is deprecated!

Formated Strings

A more flexable way of creating a string, using C style format flags follows:

 NSString *myString = [NSString stringWithFormat: @"This is an%@ of a string with %d integer.", @" example", 1];

Getting a C String

You can convert an NSString object to a plain C string using the cStringUsingEncoding: method. For example:

 NSString *newString = @"This is a test string.";
 char     *theString;

 theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];

or:

 theString = [newString UTF8String];

Note that using [string cString] is deprecated!

Reading and Writing Files With String

The following example shows how you would use files with NSString:

 // Read in the file.
 NSString *aString = [NSString stringWithContentsOfFile: @"infile.txt"];
 // Do something with our new string.
 aString = doSomething(aString);
 // And finally save our changes to a file.
 if([aString writeToFile: @"outfile" atomically: YES])
   // Success code.
 else
   // Error code.

Substrings

If you have a delimiter for breaking the string up, the easiest method to use would be the [string componentsSeparatedByString:] method. The following example demonstrates this:

 NSString *string = @"John, Bob, Jane";
 NSArray *strings = [string componentsSeparatedByString: @", "];
 // give us the array {@"John", @"Bob", @"Jane"}

An array of string can also be joined together with the [array componentsJoinedByString:] method as follows:

 NSArray *strings = [NSArray arrayWithObjects: @"John", @"Bob", @"Jane", nil];
 NSString *string = [strings componentsJoinedByString: @", "];
 // creating the string @"John, Bob, Jane"

If you need something more advanced NSScanner will be need.


One the other hand, if you know the indexes ahead of time one of three substring methods could be used, as demonstrated in the following example:

 NSString *string = @"John, Bob, Jane";
 
 NSLog([string substringFromIndex: 6]);
 // Get the last six characters in the string (", Jane")
 
 NSLog([string substringToIndex: 6]);
 // Get the first six characters in the string ("John, ")
 
 NSLog([string substringWithRange: NSMakeRange(3, 6)]);
 // Starting at index three get a string of length six ("hn, Bo")

[NSRange] is a structure used mainly in string and arrays, defining the beginning index and the offset.