當前位置:首頁 » 編程語言 » java字元轉字元串

java字元轉字元串

發布時間: 2022-01-16 09:00:14

java字元串轉為字元

代碼如下:

importjava.util.Arrays;
importjava.util.Scanner;

publicclassApp{

publicstaticvoidmain(String[]args){

Scannerscanner=newScanner(System.in);

//1.鍵盤錄入一個字元串
Stringstr=scanner.nextLine();

char[]chars=newchar[str.length()];

//2.將該字元串變成字元數組(不能使用toCharArray()方法)
for(inti=0;i<str.length();i++){

charch=str.charAt(i);

//5.將字元數組中索引為偶數的元素變成'~'
ch=(i%2==0)?'~':ch;

//3.將字元數組中的所有大寫字母變成小寫字母(不能使用toLowerCase()方法)
ch=(ch>='A'&&ch<='Z')?(char)(ch-32):ch;

chars[i]=ch;
}

//4.如果第一位和最後一位的內容不相同,則交換
if(chars[0]!=chars[chars.length-1]){

charch=chars[0];
chars[0]=chars[chars.length-1];
chars[chars.length-1]=ch;
}

//6.列印數組元素的內容
System.out.println(Arrays.toString(chars));
}
}

❷ java編程:輸入一個字元串,將其轉換為另一個字元串。

Stringstr="B";
chars=str.charAt(0);
charb=(char)(65+(90-(int)s));
System.out.println(b);

❸ java 數字轉換成字元串

各種數字類型轉換成字元串型:

String s = String.valueOf( value); // 其中 value 為任意一種數字類型。

字元串型轉換成各種數字類型:

String s = "169";
byte b = Byte.parseByte( s );
short t = Short.parseShort( s );
int i = Integer.parseInt( s );
long l = Long.parseLong( s );
Float f = Float.parseFloat( s );
Double d = Double.parseDouble( s );

數字類型與數字類對象之間的轉換:

byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();

short t = 169;
Short to = new Short( t );
t = to.shortValue();

int i = 169;
b = bo.byteValue();

short t = 169;
Short to = new Short( t );
t = to.shortValue();

int i = 169;
Integer io = new Integer( i );
i = io.intValue();

long l = 169;
Long lo = new Long( l );
l = lo.longValue();

float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();

double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();

❹ 如何用將字元數組轉化成字元串 java

//以數組的形式轉成字元串
String [] arr = {"o1","2yy","ax","0"};
String str = Arrays.toString(arr);
System.err.println(str);//輸出:[o1, 2yy, ax, 0]

//把每個元素按次序拼接轉成字元串
StringBuffer str2 = new StringBuffer();
for (String string : arr) {
str2.append(string);
}
System.err.println(str2.toString());//輸出:o12yyax0

❺ JAVA中怎麼把字元串轉換成字元數組

JAVA中把字元串轉換成字元數組的方法:java中通常用split()分割字元串,返回的是一個數組。

❻ java 如何將字元流中的字元轉換成字元串

那為何不直接使用byte[]讀呢!

FileInputStream fin = new FileInputStream("test.txt");
byte[] bytes=new byte[4096];
int b;
while((b=fin.read(bytes))>0){
//讀到b個位元組到bytes[]裡面了,這里可以處理了。

}

❼ java中怎麼將數字轉換成字元串

toString()方法轉換成字元串。

❽ java中怎樣把字元串轉換為字元

java中如何將字元串數組轉換成字元串(轉)
如果是 「字元串數組」 轉 「字元串」,只能通過循環,沒有其它方法
String[] str = {"abc", "bcd", "def"};
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length; i++){
sb. append(str[i]);
}
String s = sb.toString();

如果是 「字元數組」 轉 「字元串」 可以通過下邊的方法
char[] data={'a','b','c'};
String s=new String(data);

❾ java中怎麼把字元串轉化為方法

不明白你的意思,字元串是字元串,方法是方法。兩碼事

❿ java中如何將字元數組轉換成字元串,以逗號分割

方法1:

需要引入Apache Commons組件中的個commons-lang.jar包

String str1=StringUtils.join(ary, ",");

方法2:

格式化

String str2 = String.format("%s,%s,%s", ary);

(10)java字元轉字元串擴展閱讀:

反之,字元串轉化為字元串數組

語法:stringObject.split(separator,howmany)

例子:

<script type="text/javascript">

var str="How are you doing today?"

document.write(str.split(" ") + "<br />")

document.write(str.split("") + "<br />")

document.write(str.split(" ",3))

</script>

輸出:

How,are,you,doing,today?

H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

How,are,you

註:如果把空字元串 ("") 用作separator,那麼 stringObject 中的每個字元之間都會被分割。String.split() 執行的操作與Array.join執行的操作是相反的。

熱點內容
酒店配置什麼滅火系統 發布:2025-02-09 08:06:37 瀏覽:773
java至尊 發布:2025-02-09 08:03:23 瀏覽:558
pythonwith 發布:2025-02-09 08:00:25 瀏覽:172
Ftp打開文件是只讀模式 發布:2025-02-09 07:40:55 瀏覽:504
androidlistview點擊事件 發布:2025-02-09 07:25:52 瀏覽:171
targz解壓縮 發布:2025-02-09 06:59:19 瀏覽:311
wpsphp 發布:2025-02-09 06:58:41 瀏覽:962
視易鋒雲系統如何架設輔助伺服器 發布:2025-02-09 06:47:08 瀏覽:770
mysql備份腳本shell 發布:2025-02-09 06:46:33 瀏覽:15
騰訊雲伺服器怎樣調整解析度 發布:2025-02-09 06:46:30 瀏覽:369