Java字符串整理
一、字符串的輸入與轉換:
in.next(); //到空格就結束
in.nextLine();
String str = new String();
1.一維:
(1)空格不當做字符串的:
String g[] = new String[n]; //字符串數組
for(int i=0;i<n;i++)
g[i] = in.next(); //字符串類型的,到空格結束(每一個可以不只是單個字符)
輸入:fasf fag agha 53
輸出:fasf fag agha 53
String str = new String();
char s[] = new char[n];
str = in.next(); //如果輸入的有空格的話,到第一個空格就結束了
for(int i=0;i<str.length();i++) //str.length() = 1;
s[i] = str.charAt(i);
輸入:fasf fag agha 53
輸出:fasf
//不帶空格的字符串。
輸入:hfjkdn
輸出:hfjkdn
(2)空格字符串:
char s[] = new char[150];
String str = new String();
str = in.nextLine(); //輸入一行字符
System.out.println(str.length());
for(int i=0;i<str.length();i++)
s = str.toCharArray(); //轉換成單個字符
或者:(兩種結果一樣)
String str = new String();
str = in.nextLine(); //輸入一行字符
System.out.println(str.length());
char s[] = str.toCharArray();
輸入:gfoijrskjdfbm
輸出:13
g f o i j r s k j d f b m
輸入:iuegnf asfdhan akfje
輸出:20
i u e g n f a s f d h a n a k f j e
2.二維:
String s[][] = new String[n][n]; //二維字符數組
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
s[i][j] = in.next();
}
}
輸入: 1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
輸出: 1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
String str = new String();
char s[][] = new char[][];
for(int i=0;i<n;i++)
{
str = in.next(); //輸入一行字符串沒有空格的
for(int j=0;j<n;j++)
{
s[i][j] = str.charAt(j);
}
}
輸入:
...#
..#.
.#..
#...
輸出:
. . . #
. . # .
. # . .
# . . .
二、類型轉換:
1.字符串轉換成整形:Integer.parseInt(str);//括號裏面爲字符串
2.字符串轉換成double形:Double.parseDouble(str);//括號裏面數字字符串轉換成double型
3.
//最終結果都是十進制數
static int parseInt(String s) //返回用十進制參數表示的整數值。
static int parseInt(String s, int radix) //使用指定基數的字符串參數表示的整數 (基數可以是 10, 2, 8, 或 16 等進制數) 。
public class Test{
public static void main(String args[]){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}
編譯以上程序,輸出結果爲:
9
5.0
1092 (將十六444 轉換成十進制=1092)
Double.parseDouble()與Double.valueOf()區別:
in.next(); //到空格就結束
in.nextLine();
String str = new String();
1.一維:
(1)空格不當做字符串的:
String g[] = new String[n]; //字符串數組
for(int i=0;i<n;i++)
g[i] = in.next(); //字符串類型的,到空格結束(每一個可以不只是單個字符)
輸入:fasf fag agha 53
輸出:fasf fag agha 53
String str = new String();
char s[] = new char[n];
str = in.next(); //如果輸入的有空格的話,到第一個空格就結束了
for(int i=0;i<str.length();i++) //str.length() = 1;
s[i] = str.charAt(i);
輸入:fasf fag agha 53
輸出:fasf
//不帶空格的字符串。
輸入:hfjkdn
輸出:hfjkdn
(2)空格字符串:
char s[] = new char[150];
String str = new String();
str = in.nextLine(); //輸入一行字符
System.out.println(str.length());
for(int i=0;i<str.length();i++)
s = str.toCharArray(); //轉換成單個字符
或者:(兩種結果一樣)
String str = new String();
str = in.nextLine(); //輸入一行字符
System.out.println(str.length());
char s[] = str.toCharArray();
輸入:gfoijrskjdfbm
輸出:13
g f o i j r s k j d f b m
輸入:iuegnf asfdhan akfje
輸出:20
i u e g n f a s f d h a n a k f j e
2.二維:
String s[][] = new String[n][n]; //二維字符數組
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
s[i][j] = in.next();
}
}
輸入: 1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
輸出: 1 SUM(2,1:3,1)
2 AVG(1,1:1,2)
SUM(1,1:2,1) STD(1,1:2,2)
String str = new String();
char s[][] = new char[][];
for(int i=0;i<n;i++)
{
str = in.next(); //輸入一行字符串沒有空格的
for(int j=0;j<n;j++)
{
s[i][j] = str.charAt(j);
}
}
輸入:
...#
..#.
.#..
#...
輸出:
. . . #
. . # .
. # . .
# . . .
二、類型轉換:
1.字符串轉換成整形:Integer.parseInt(str);//括號裏面爲字符串
2.字符串轉換成double形:Double.parseDouble(str);//括號裏面數字字符串轉換成double型
3.
//最終結果都是十進制數
static int parseInt(String s) //返回用十進制參數表示的整數值。
static int parseInt(String s, int radix) //使用指定基數的字符串參數表示的整數 (基數可以是 10, 2, 8, 或 16 等進制數) 。
public class Test{
public static void main(String args[]){
int x =Integer.parseInt("9");
double c = Double.parseDouble("5");
int b = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(c);
System.out.println(b);
}
}
編譯以上程序,輸出結果爲:
9
5.0
1092 (將十六444 轉換成十進制=1092)
Double.parseDouble()與Double.valueOf()區別:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.