2011年3月31日 星期四

combine String

目前只學過java、js、actionscript3、c、objective-C 學過以後就覺得,後兩者在string(char * / NSString)處理上真不是普通麻煩。 要combine String的話,各是這樣:

Java

public class StringTest {
	public static void main(String[] args) {
		String str1 = "This is a string.\n";
		String str2 = "This is a string2.\n";

		str1 += str2;
		System.out.print(str1);
	}
}

javascript

var str1 = "This is a string.\n";
var str2 = "This is a string2.\n";
str1 += str2;
document.write(str1);

ActionScript3

var str1:String = "This is a string.\n";
var str2:String = "This is a string2.\n";
str1 += str2;
trace(str1);

C

因為還沒學過,所以只貼上自己找到的資料:C Gossip: 字串長度、複製、串接 From Gossip@caterpillar

Objective-C

這個是我覺得最麻煩的…… 還要考慮到formate
#import <stdio.h>
#import <Foundation/NSString.h>

int main( int argc, char *argv[] ) {
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	NSString str1 = @"This is a string.\n";
	NSString str2 = @"This is a string2.\n";

	str1 = [NSString stringWithFormat: @"%@%@", str1, str2];

	NSLog(@"%@\n", str1);
	//或者是
	printf("%s\n", [str1 UTF8String]);
	[pool release];
	return EXIT_SUCCESS;
}
 

沒有留言:

張貼留言