# 将字符串转换为Int

## 使用案列

`235`
`int n = 235`

`1ab`
`int n =  0   (as the string contains characters other than digits output is 0)`

## C ++程序

### 使用字符串流 将字符串转换为int

#### 算法

```1. Initialise a string s of size n.
2. Initialise an object of the string stream class.
3. After that, create a variable of integer type and stream the object with the integer.
4. Print the integer variable.```

#### C ++代码

```#include <iostream>
#include <sstream>
using namespace std;

int main(){
string s = "235";

stringstream str(s);

int n = 0;
str>>n;

cout<<"int n = "<<n;

return 0;
}```
`int n = 235`

### 使用sscanf（） 将字符串转换为int

#### 算法

```1. Initialise a const char str.
2. After that, initialise an object of the string stream class.
3. Create an variable of integer type and update the value of the integer variable using the sscanf.
4. Print the Integer variable.```

#### C ++代码

```#include <iostream>
#include <sstream>
using namespace std;

int main(){

const char *str = "235";
int n;
sscanf(str, "%d", &n);

cout<<"int n = "<<n;

return 0;
}```
`int n = 235`

### 使用stoi（） 将字符串转换为int

stoi（）接受字符串并返回其值。

#### 算法

```1. Initialise a string s of size n.
2. Create a variable of the integer type and update the value of the integer variable wih the function stoi().
3. Print the Integer variable.```

#### C ++代码

```#include <iostream>
#include <string>
using namespace std;

int main(){

string s = "235";

int n = stoi(s);

cout<<"int n = "<<n;
return 0;
}```
`int n = 235`

### 使用atoi（） 将字符串转换为int

atoi（）接受字符数组或字符串并返回其值。

#### 算法

```1. Initialise an array of character type or a string literal s.
2. After that, create a variable of integer type and update the value of integer variable with the function atoi().
3. Print the Integer variable.```

#### C ++代码

```#include <iostream>
#include <string>
using namespace std;

int main(){

const char *s = "235";

int n = atoi(s);

cout<<"int n = "<<n;
return 0;
}```
`int n = 235`

## Java程序将字符串转换为int

### 使用Integer.parseInt（）

#### 算法

```1. Initialise a string s of size n.
2. After that, create a variable of integer type and update the value of the integer variable as the parsed value of string to integer using the method Integer.parseInt(s).
3. Print the Integer variable.```

#### Java代码

```import java.io.*;

class convertStr{

public static int convert(String s){
int n = 0;
try{
n = Integer.parseInt(s);
}
catch(NumberFormatException e){
}

return n;
}

public static void main(String[] args){

String s = "235";
int n = convert(s);
System.out.println("int n = "+n);

}
}```
`int n = 235`

### 使用的Ints :: tryParse方法 番石榴图书馆

#### 算法

```1. Initialise a string s of size n and convert it into int using Ints::tryParse method.
2. Create a variable of the integer type and update the value of integer variable as the result.
3. Print the Integer variable.```

#### Java代码

```import java.io.*;
import java.util.*;

import java.io.*;

class convertStr{

public static int convert(String s){
int n = 0;

n = Optional.ofNullable(s).map(Ints::tryParse).orElse(0);

return n;
}

public static void main(String[] args){

String s = "235";
int n = convert(s);
System.out.println("int n = "+n);

}
}```
`int n = 235`