"String을 int로 변환"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 52개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[category: conversion]]
{{다른뜻|정수형}}
[[category: 형 변환]]
[[category: String]]
[[category: String]]
[[category: Int]]
[[category: Int]]
;string-int 형 변환
;String to int
;String to int
;String to integer
;String to integer
7번째 줄: 9번째 줄:
;parseInt
;parseInt
;Integer.parseInt
;Integer.parseInt
==같이 보기==
* [[int to string]]
* [[removing leading zero]]
* [[string to number]]
* [[extract number]]
* [[String 배열을 int 배열로 변환]]
==C==
[[분류: C]]
{{참고|C언어 atoi()}}
<syntaxhighlight lang='c' run>
#include <stdio.h>
#include <stdlib.h>
int main() {
    char *str = "123";
    printf("%d\n", atoi(str)); // 123
    return 0;
}
</syntaxhighlight>
==C++==
[[분류: C++]]
{{참고|C++ String을 int로 변환}}
<syntaxhighlight lang='cpp' run>
#include <iostream>
using namespace std;
int main() {
string str = "123";
int n = stoi(str);
cout << n; // 123
}
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang='csharp'>
<syntaxhighlight lang='csharp' run>
String str = "123";
using System;class Z{static void Main(){
 
string str = "123";
int i = int.Parse(str);
int i = int.Parse(str);
</source>
Console.WriteLine(i);
 
}}
</syntaxhighlight>
 
==Go==
{{참고|Go string-int 형 변환}}
[[category: Go]]
<syntaxhighlight lang='go' run>
package main
 
import "fmt"
import "strconv"
 
func main() {
var i int
i, _ = strconv.Atoi("42")
fmt.Println(i)
}
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 String-int 형 변환}}
[[category: Java]]
[[category: Java]]
<source lang='java'>
<syntaxhighlight lang='java' run>
String str = "123";
class MyClass
int i = Integer.parseInt(str);
{
</source>
    public static void main(String[] args) {
        String str = "123";
        int i = Integer.parseInt(str);
        System.out.println(i);
    }
}
</syntaxhighlight>


==Javascript==
==JavaScript==
[[category: javascript]]
[[category: JavaScript]]
<source lang='javascript'>
{{참고|JavaScript 문자열을 int로 변환}}
parseInt('123.45')  // 123
<syntaxhighlight lang='javascript' run>
parseInt('77')      // 77
console.log( parseInt('123.45') ); // 123
</source>
console.log( parseInt('77') );     // 77
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
NSString *s = @"42";
NSString *s = @"42";
int i = [s intValue];
int i = [s intValue];
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl' run>
print 0 + '42';                       # 42
print 0 + '42';       # 42
</source>
print int '42,000';  # 42
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
echo intval('42');                   // 42
echo intval('42')."\n";           // 42
echo intval('+42');                   // 42
echo intval('+42')."\n";           // 42
echo intval('-42');                   // -42
echo intval('-42')."\n";           // -42
 
echo intval('42,000')."\n";       // 42
echo intval('42,000');               // 42
</syntaxhighlight>
 
<syntaxhighlight lang='php' run>
echo intval(42);                     // 42
echo intval(42)."\n";             // 42
echo intval(4.2);                     // 4
echo intval(4.2)."\n";             // 4
echo intval(042);                     // 34
echo intval(042)."\n";             // 34
echo intval('042');                   // 42
echo intval('042')."\n";           // 42
echo intval(1e10);                   // 1410065408
echo intval(1e10)."\n";           // 10000000000
echo intval('1e10');                 // 1
echo intval('1e10')."\n";         // 10000000000
echo intval(0x1A);                   // 26
echo intval(0x1A)."\n";           // 26
 
</syntaxhighlight>
echo 0 + '42';                       // 42
<syntaxhighlight lang='php' run>
</source>
var_dump( 0 + '42' );                // int(42)
var_dump( intval('hello42') );        // int(0)
var_dump( intval('42hello') );       // int(42)
</syntaxhighlight>
<syntaxhighlight lang='PHP' run>
function string_to_int($str) { return (int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$str); }
var_dump( string_to_int( 'The value is 0083.123' ) ); // int(83)
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='python'>
<syntaxhighlight lang='python' run>
s = "123"
s = "123"
i = int(s)
i = int(s)
</source>
print( type(i) )
print( i )
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='ruby'>
<syntaxhighlight lang='ruby' run>
str = "123"
str = "123"
i = str.to_i
i = str.to_i
</source>
print i
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang='vb'>
<syntaxhighlight lang='vb'>
Dim s As String
Dim s As String
Dim i As Integer
Dim i As Integer
84번째 줄: 159번째 줄:
s = "42"
s = "42"
i = Val(s)
i = Val(s)
</source>
</syntaxhighlight>
 
==같이 보기==
*[[int to string]]
*[[removing leading zero]]
*[[string to number]]
*[[extract number]]

2023년 11월 1일 (수) 22:40 기준 최신판

  다른 뜻에 대해서는 정수형 문서를 참조하십시오.
string-int 형 변환
String to int
String to integer
atoi
parseInt
Integer.parseInt

1 같이 보기[ | ]

2 C[ | ]

#include <stdio.h>
#include <stdlib.h>
int main() {
    char *str = "123";
    printf("%d\n", atoi(str)); // 123
    return 0;
}

3 C++[ | ]

#include <iostream>
using namespace std;
int main() {
	string str = "123";
	int n = stoi(str);
	cout << n; // 123	
}

4 C#[ | ]

using System;class Z{static void Main(){

string str = "123";
int i = int.Parse(str);
Console.WriteLine(i); 

}}

5 Go[ | ]

package main

import "fmt"
import "strconv"

func main() {
	var i int
	i, _ = strconv.Atoi("42")
	fmt.Println(i)
}

6 Java[ | ]

class MyClass
{
    public static void main(String[] args) {
        String str = "123";
        int i = Integer.parseInt(str);
        System.out.println(i);
    }
}

7 JavaScript[ | ]

console.log( parseInt('123.45') );  // 123
console.log( parseInt('77') );      // 77

8 Objective-C[ | ]

NSString *s = @"42";
int i = [s intValue];

9 Perl[ | ]

print 0 + '42';       # 42
print int '42,000';   # 42

10 PHP[ | ]

echo intval('42')."\n";            // 42
echo intval('+42')."\n";           // 42
echo intval('-42')."\n";           // -42
echo intval('42,000')."\n";        // 42
echo intval(42)."\n";              // 42
echo intval(4.2)."\n";             // 4
echo intval(042)."\n";             // 34
echo intval('042')."\n";           // 42
echo intval(1e10)."\n";            // 10000000000
echo intval('1e10')."\n";          // 10000000000
echo intval(0x1A)."\n";            // 26
var_dump( 0 + '42' );                 // int(42)
var_dump( intval('hello42') );        // int(0)
var_dump( intval('42hello') );        // int(42)
function string_to_int($str) { return (int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$str); }
var_dump( string_to_int( 'The value is 0083.123' ) ); // int(83)

11 Python[ | ]

s = "123"
i = int(s)
print( type(i) )
print( i )

12 Ruby[ | ]

str = "123"
i = str.to_i
print i

13 VB[ | ]

Dim s As String
Dim i As Integer

s = "42"
i = Val(s)