mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
Test mutable strings and "strings as collections of char's".
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@243 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d8483a782f
commit
0ed2e973e7
1 changed files with 34 additions and 2 deletions
|
@ -1,10 +1,42 @@
|
||||||
#include "objects/String.h"
|
#include <foundation/NSString.h>
|
||||||
|
|
||||||
|
/* For demo of Strings as Collections of char's. */
|
||||||
|
#include <objects/NSString.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
print_string(NSString* s)
|
||||||
|
{
|
||||||
|
printf("The string [%s], length %d\n", [s cString], [s length]);
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
id s = @"This is a test string";
|
id s = @"This is a test string";
|
||||||
id s2;
|
id s2;
|
||||||
|
|
||||||
printf("The string [%s], length %d\n", [s cString], [s length]);
|
print_string(s);
|
||||||
|
|
||||||
|
s2 = [s stringByAppendingString:@" with something added"];
|
||||||
|
print_string(s2);
|
||||||
|
|
||||||
|
s2 = [s mutableCopy];
|
||||||
|
[s2 replaceCharactersInRange:((NSRange){10,4})
|
||||||
|
withString:@"changed"];
|
||||||
|
print_string(s2);
|
||||||
|
|
||||||
|
/* An example of treating a string like a Collection:
|
||||||
|
Increment each char. */
|
||||||
|
{
|
||||||
|
id s3;
|
||||||
|
void rot13(elt c)
|
||||||
|
{
|
||||||
|
[s3 appendElement:(char)(c.char_u + 1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
s3 = [NSMutableString stringWithCapacity:[s2 length]];
|
||||||
|
[s2 withElementsCall:rot13];
|
||||||
|
print_string(s3);
|
||||||
|
}
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue