티스토리 뷰

Iphone

문자열처리

잠덜 2012.01.10 16:19

+ ( id ) stringWithFormat: (NSString *) format, ....;

- ( unsigned int )length;
stringByAppending... 뒤에 추가 시리즈
substring... 뒤에, 앞에, 지정영역 자르기

- ( BOOL ) hasPrefix: (NSString *) string; //인자가 리시버 앞부분과일치하는지
- ( BOOL ) hasSuffix: (NSString *) string; //뒷부분
- ( NSRange ) rangeOfString: (NSString *)string; //부분문자열검색
- ( NSRange ) rangeOfString: (NSString *)string options:(NSStringCompareOptions)mask
- ( NSArray ) componentSeperateByString: (NSString *)string; //인자를토큰으로하여분리한문자열배열을만든다.
- ( BOOL ) isEqualToString: (NSString *)string;
- ( NSComparisonResult) compare: (NSString *) string;
- ( NSComparisonResult) compare: (NSString *) string option: (unsigned) mask;

 boolValue, intValue 등 숫자관련 메서드 존재함


typedef enum _NSComparisonResult{
  NSOrderedAscending = -1, (원본 < 비교 대상일 경우 ex) 원본:'a' 비교:'c')
  NSOrderedSame
 
NSOrderedDescending
}NSComparisonResult;

 mask
- NSCaseInsensitiveSearch : 대소문자를 무시하고 문자열을 비교한다.
- NSLiteralSearch : 대소문자를 구분하여 문자열을 비교한다.
- NSNumericSearch : 숫자문자를 숫자 자체로 인식( Filename9.txt < Filename20.txt < Filename100.txt )  
- NSBackwardsSearch : 문자열의 뒤에서 부터 문자열을 비교한다.
- NSAnchoredSearch : 문자열의 시작(NSBackwardsSearch 지정시 ) 부분과 비교대상이 일치하는지 비교한다.(hasPrefix 같음)

 

 

 

rangeOfString 예제)

 

NSString *searchString = @"age";

 

NSString *beginsTest = @"Agencies";

 

NSRange prefixRange = [beginsTest rangeOfString:searchString options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];

 

//결과 prefixRange = { 0, 3 }

 


 

 

NSMutableString 

 

+ ( id ) stringWithCapacity: (unsigned)capacity;

 

- ( void ) appendString: (NSString*)string;

 

- ( void ) appendFormat: (NSString *) format, ...;

 

- ( void ) deleteCharactersInRange: (NSRange)range;

 

 

 

 

 

 

 

 

pecifier

Description

%@

Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.

%%

'%' character

%d%D,%i

Signed 32-bit integer (int)

%u%U

Unsigned 32-bit integer (unsigned int)

%hi

Signed 16-bit integer (short)

%hu

Unsigned 16-bit integer (unsigned short)

%qi

Signed 64-bit integer (long long)

%qu

Unsigned 64-bit integer (unsigned long long)

%x

Unsigned 32-bit integer (unsigned int), 16진수

%X

Unsigned 32-bit integer (unsigned int), 16진수(알파벳 대문자로)

%qx

Unsigned 64-bit integer (unsigned long long), 16진수

%qX

Unsigned 64-bit integer (unsigned long long), 16진수(알파벳 대문자로)

%o%O

Unsigned 32-bit integer (unsigned int), 8진수

%f

64-bit floating-point number (double)

%e

64-bit floating-point number (double), 공학 표현법(ex:3.15134e-32)

%E

64-bit floating-point number (double), 공학 표현법(알파벳 대문자)

%g

64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise

%G

64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise

%c

8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format\\ddd or the Unicode hexadecimal format \\udddd, where d is a digit

%C

16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\dddor the Unicode hexadecimal format \\udddd, where d is a digit

%s

Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.

%S

Null-terminated array of 16-bit Unicode characters

%p

Void pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x

%L

Length modifier specifying that a following aAeEfFg, or G conversion specifier applies to a long double argument

%a

64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent

%A

64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent

%F

64-bit floating-point number (double), printed in decimal notation

%z

Length modifier specifying that a following dioux, or X conversion specifier applies to a size_t or the corresponding signed integer type argument

%t

Length modifier specifying that a following dioux, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument

%j

Length modifier specifying that a following dioux, or X conversion specifier applies to a intmax_t or uintmax_t argument

 

 

 

그 외

NSString* str1 = @”Objective-C 2.0 Programming. “;
NSString* str2 = @”xCode is Powerful. “;
NSString* str3;
NSString* strNum = @”7.7″;
NSString* tmp;
NSMutableString* mutableString;

NSComparisonResult compareResult;
NSRange range;

// 빈 문자열 생성str3 = [NSString string];

// 문자열로 새로운 문자열 생성str3 = [NSString stringWithString:@"stringWithString"];
NSLog(@”%@”, str3);

str3 = [[NSString alloc] initWithString:@”initWithString”];
NSLog(@”%@”, str3);
[str3 release];

// 문자열 길이NSLog(@”length of %@ : %i”, str1, [str1 length]);
NSLog(@”length of %@ : %i”, str2, [str2 length]);

// 문자열 복사tmp = [NSString stringWithString:str1];
NSLog(@”tmp : %@”, tmp);

// 문자열 합치기tmp = [str1 stringByAppendingString:str2];
NSLog(@”%@ + %@ = %@”, str1, str2, tmp);

// 문자열 비교tmp = str1;
if ( [str1 isEqualToString:tmp] == YES )
NSLog(@”%@ == %@”, tmp, str1);
else
NSLog(@”%@ != %@”, tmp, str1);

// 문자열 비교(대소문자 구분)compareResult = [str1 compare:str2];
if (compareResult == NSOrderedAscending)
NSLog(@”%@ < %@”, str1, str2);
else if (compareResult == NSOrderedSame)
NSLog(@”%@ == %@”, str1, str2);
else
NSLog(@”%@ > %@”, str1, str2);

// 문자열 비교(대소문자 구분하지 않음)compareResult = [str1 caseInsensitiveCompare:str2];
if (compareResult == NSOrderedAscending)
NSLog(@”%@ < %@”, str1, str2);
else if (compareResult == NSOrderedSame)
NSLog(@”%@ == %@”, str1, str2);
else
NSLog(@”%@ > %@”, str1, str2);

// 첫문자가 대문자이고 나머지가 소문자인 문자열 리턴tmp = [str2 capitalizedString];
NSLog(@”%@”, tmp);

// 대문자로 변환tmp = [str1 uppercaseString];
NSLog(@”%@ -> %@”, str1, tmp);

// 소문자로 변환tmp = [str2 lowercaseString];
NSLog(@”%@ -> %@”, str2, tmp);

// UTF-8 형식의 문자열 리턴const char* utf8str = [str1 UTF8String];

// 특정 인덱스의 유니코드 문자 반환unichar ch = [str2 characterAtIndex:7];
NSLog(@”%c”, ch);

// 특정 인덱스까지의 문자열 반환tmp = [str1 substringToIndex:15];
NSLog(@”%@”, tmp);

// 특정 인덱스부터 문자열 끝까지 반환tmp = [str2 substringFromIndex:9];
NSLog(@”%@”, tmp);

// 문자열 중간부분 반환tmp = [str1 substringWithRange:NSMakeRange(7, 14)];
NSLog(@”%@”, tmp);

// 문자열 내부 검색range = [str2 rangeOfString:@"Power"];
if (range.location == NSNotFound)
NSLog(@”not found”);
else
NSLog(@”at index %i, length %i”, range.location, range.length);

// 문자열이 특정 문자열로 시작하는지 확인BOOL bRet = [str1 hasPrefix:@"Obj"];
if (bRet)
NSLog(@”Obj로 시작하는 문자열”);

// 문자열이 특정 문자열로 끝나는지 확인bRet = [str2 hasSuffix:@"Power"];
if (bRet)
NSLog(@”Power 로 끝나는 문자열”);

// 문자열을 double 값으로 변환double dblValue = [strNum doubleValue];
NSLog(@”%g”, dblValue);

// 문자열을 float 값으로 변환float fValue = [strNum floatValue];
NSLog(@”%f”, fValue);

// 문자열을 int 값으로 변환int iValue = [strNum intValue];
NSLog(@”%i”, iValue);

// 임의의 길이를 가진 빈 문자열 생성mutableString = [NSMutableString stringWithCapacity:10];
mutableString = [[NSMutableString alloc] initWithCapacity:10];
[mutableString release];

mutableString = [NSMutableString stringWithString:str2];

// 특정 인덱스 위치에 문자열 넣기[mutableString insertString:@"most " atIndex:9];
NSLog(@”%@”, mutableString);

// 문자열 끝에 새로운 문자열 붙이기[mutableString appendString:@" dev tool!!"];
NSLog(@”%@”, mutableString);

// 특정 범위 삭제[mutableString deleteCharactersInRange:NSMakeRange(23, 12)];
NSLog(@”%@”, mutableString);

// 특정 문자열 검색 후 삭제range = [mutableString rangeOfString:@"Power"];
if (range.location != NSNotFound)
{
[mutableString deleteCharactersInRange:range];
NSLog(@”%@”, mutableString);
}

// 문자열 설정[mutableString setString:@"xCode is Powerful. "];
NSLog(@”%@”, mutableString);

// 특정 범위를 새로운 문자열로 대치[mutableString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"Apple xCode"];
NSLog(@”%@”, mutableString);

// 특정 문자를 모두 대치NSString* searchWord = @”o”;
NSString* replaceWord = @”0″;
[mutableString replaceOccurrencesOfString:searchWord withString:replaceWord options: (NSStringCompareOptions)nil range:NSMakeRange(0, [mutableString length])];
NSLog(@”%@”, mutableString);

저작자 표시
신고

'Iphone' 카테고리의 다른 글

문자열처리  (0) 2012.01.10
댓글
댓글쓰기 폼