1
2
3
4
5
|
String str =
"今天天气不错"
;
int
index = str.indexOf(
"天气"
);
System.out.println(index);
// 大于0 则表示存在 为-1 则表示不存在
String s = str.replace(
"天气"
,
"心情"
);
System.out.println(s);
// 输出“今天心情不错
|
String s1=URLEncoder.encode(s, "utf-8");
int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
以下是答案:
第一种方法:s=i+""; //会产生两个String对象 第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常 |
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
public
static
void
main(String[] args) {
String s =
"ab cde fg"
;
String[] re = s.split(
" "
);
//用split()函数直接分割
for
(String string : re) {
System.out.println(string);
}
}
1
2
3
4
5
|
String str =
"今天天气不错"
;
int
index = str.indexOf(
"天气"
);
System.out.println(index);
// 大于0 则表示存在 为-1 则表示不存在
String s = str.replace(
"天气"
,
"心情"
);
System.out.println(s);
// 输出“今天心情不错
|
String s1=URLEncoder.encode(s, "utf-8");
int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
以下是答案:
第一种方法:s=i+""; //会产生两个String对象 第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常 |
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
public
static
void
main(String[] args) {
String s =
"ab cde fg"
;
String[] re = s.split(
" "
);
//用split()函数直接分割
for
(String string : re) {
System.out.println(string);
}
}