Programing

문자열을 정수로 변환

lottogame 2020. 9. 10. 08:17
반응형

문자열을 정수로 변환


문자열을 정수로 변환하는 데 문제가 있습니다. 나는 그것을 봤지만 내가 찾을 수있는 것은 int를 문자열로 변환하는 방법입니다. 다른 방법으로 수행하는 방법을 아는 사람이 있습니까? 감사.


참고 항목 있는 NSString 클래스 참조 .

NSString *string = @"5";
int value = [string intValue];

어때

[@"7" intValue];

또한 원하는 경우 NSNumber할 수 있습니다.

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter numberFromString:@"7"];

나는 사용한다:

NSInteger stringToInt(NSString *string) {
    return [string integerValue];
}

그 반대:

NSString* intToString(NSInteger integer) {
    return [NSString stringWithFormat:@"%d", integer];
}

스위프트 3.0 :

Int("5")

또는

let stringToConvert = "5"
Int(stringToConvert)

이것은 문자열을 int로 변환하는 간단한 솔루션입니다.

NSString *strNum = @"10";
int num = [strNum intValue];

그러나 당신이 때 텍스트 필드에서 값을 받고 다음,

int num = [txtField.text intValue];

여기서 txtFieldUITextField 의 출력입니다.


나는 이와 같은 일을해야했지만 getter / setter를 사용하고 싶었습니다. 특히 텍스트 필드에서 long을 반환하고 싶었습니다. 다른 답변도 모두 잘 작동했으며 학교 프로젝트가 발전함에 따라 약간의 적응으로 끝났습니다.

long ms = [self.textfield.text longLongValue];
return ms;

NSString *string = /* Assume this exists. */;
int value = [string intValue];

아주 쉽게..

int (name of integer) = [(name of string, no ()) intValue];


또 다른 방법 : 예를 들어 const char *와 같이 C 문자열로 작업하는 경우 C native atoi()가 더 편리합니다.


다음과 같이 사용할 수도 있습니다.

NSInteger getVal = [self.string integerValue];


String 숫자를 Int 로 변환하려면 다음을 수행해야합니다.

let stringNumber = "5"
let number = Int(stringNumber)

참고 URL : https://stackoverflow.com/questions/6638085/convert-a-string-into-an-int

반응형