#include<iostream>#include<string>usingnamespace std;intmain(){
string s ="153";char* c;int i;double d;/* string 类型分别与其他类型进行转化 */
c = s.c_str();
i =stoi(s);
d =stod(s);
cout << s <<" "<< c <<" "<< i <<" "<< d << endl;/* 输出 */return0;}
基本类型 转 string 使用方法 to_string(val)
voidTypeConvert(){/* 其他基本类型 */int i =5;double d =3.14;char*p ="helloworld"; string s;
cout <<to_string(i)<<" "<<to_string(d)<< endl;
s = p; cout << s << endl;/* char* 类型直接赋值 */}
Java
String 转 int、double
/* String 转成 int 、 double 的两种方法 */String s ="123456";int i =Integer.valueOf(s).intValue();Double d =Double.valueOf(s).doubleValue();int i1 =Integer.parseInt(s);Double d1 =Double.parseDouble(s);System.out.println("%d %d",i,i1);System.out.println("%lf %lf",d,d1);