본문으로 건너뛰기
토파즈문서
6.05.21

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    다운로드 →

    라이브러리 · 문자열

    string.toInt

    Parse an optional leading minus sign and ASCII decimal digits as an int.

    toInt() -> Option<int>
    수신자
    string
    최소 인자 수
    0
    최대 인자 수
    0

    Declaration and parameters

    사용 조건

    기본 포함 · 예

    These types are provided by the language. No module import is required.

    The declarations below describe the standard operations on built-in values.

    수신자

    string

    매개변수 제약

    string.toInt() returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.

    Leading zeros are accepted. -0 produces 0.

    Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().

    A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.

    Call trim() before toInt() when the input format allows surrounding whitespace.

    결과

    The declared result type is Option<int>.

    효과

    실패

    Result and effects

    Return None() for any other spelling and for a value outside the int range.

    Related entries

    Strings →

    Parse decimal integers with one spelling

    Parse decimal integers with one spelling

    string.toInt() returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.

    Leading zeros are accepted. -0 produces 0.

    Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().

    A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.

    Call trim() before toInt() when the input format allows surrounding whitespace.

    이유

    One number has one accepted spelling, as in BigInt and Decimal text parsing. Rejecting whitespace and a plus sign keeps malformed input visible to the program, so a reader of wire formats does not accept a value that its writer never produces.

    Parse a minus sign and decimal digits

    main.tpzParse a minus sign and decimal digits플레이그라운드 열기 →
    1. import std.io
    2. function show(text: string) -> string {
    3. match (text.toInt()) {
    4. case Some(value) => "{value}"
    5. case None() => "none"
    6. }
    7. }
    8. for text in (["42", "-7", "007", "-0", "9223372036854775807", "-9223372036854775808"]) {
    9. io.writeLine("[{text}] {show(text)}")
    10. }
    기대 출력종료 코드 0
    [42] 42
    [-7] -7
    [007] 7
    [-0] 0
    [9223372036854775807] 9223372036854775807
    [-9223372036854775808] -9223372036854775808
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. [42] 42
    2. [-7] -7
    3. [007] 7
    4. [-0] 0
    5. [9223372036854775807] 9223372036854775807
    6. [-9223372036854775808] -9223372036854775808

    Return None() for other spellings and out-of-range values

    main.tpzReturn None() for other spellings and out-of-range values플레이그라운드 열기 →
    1. import std.io
    2. function show(text: string) -> string {
    3. match (text.toInt()) {
    4. case Some(value) => "{value}"
    5. case None() => "none"
    6. }
    7. }
    8. for text in (["", "-", "12a", "1_000", "0x10", "1e3", "١٢", "9223372036854775808", "-9223372036854775809"]) {
    9. io.writeLine("[{text}] {show(text)}")
    10. }
    기대 출력종료 코드 0
    [] none
    [-] none
    [12a] none
    [1_000] none
    [0x10] none
    [1e3] none
    [١٢] none
    [9223372036854775808] none
    [-9223372036854775809] none
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. [] none
    2. [-] none
    3. [12a] none
    4. [1_000] none
    5. [0x10] none
    6. [1e3] none
    7. [١٢] none
    8. [9223372036854775808] none
    9. [-9223372036854775809] none

    Return None() for surrounding whitespace and a plus sign

    main.tpzReturn None() for surrounding whitespace and a plus sign플레이그라운드 열기 →
    1. import std.io
    2. function show(text: string) -> string {
    3. match (text.toInt()) {
    4. case Some(value) => "{value}"
    5. case None() => "none"
    6. }
    7. }
    8. for text in ([" 12", "12 ", "+5"]) {
    9. io.writeLine("[{text}] {show(text)}")
    10. }
    기대 출력종료 코드 0
    [ 12] none
    [12 ] none
    [+5] none
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. [ 12] none
    2. [12 ] none
    3. [+5] none