當前位置:首頁 » 編程語言 » java截取指定字元串

java截取指定字元串

發布時間: 2023-01-29 09:15:16

java截取字元串中的指定字元

public class Demo {
public static void main(String[] args) {
String str = "wsdefidrfadsnrewfidan";
String newstr="";
for (int i = 0; i <str.length(); i++) {
char ch=str.charAt(i);
if (ch=='w'||ch=='i'||ch=='n') {
newstr+=ch;
}
}
System.out.println(newstr);
}
}

② java中如何截取字元串

截取字元串採用的是java中的split函數。
例把「01:大眾汽車」截取為01和大眾汽車,代碼如下:
package test;

public class substringTest
{
public static void main(String args[])
{
String N = "01:大汽車";
String L="";
String R="";
int k= N.length();
for (int i = 0; i < N.length(); i++)
{
if (N.substring(i, i + 1).equals("|"))
{
L=N.substring(0,i).trim();
R=N.substring(i+1,k).trim();
}
else
{

}
System.out.println(L);
System.out.println(R);
}
}
}

③ java怎麼截取指定字元之後的字元串

可以通過java的」substring「方法截取出對應的字元串,前提是知道開始和結束的字元串的值。

④ java 截取字元串

用String類的substring(int from,int to)方法去截字元串位置為from到to-1位置的字元
substring(int index)方法去截字元串位置index-1及以後的所有字元串,注意字元串的字元位置是從0開始的,substring(int from ,int to)方法是前閉後開的,即[from,to),可以理解為[from,to-1]
例:String name="helloworld";
System.out.println(name.substring(name.length()-1,name.length()));//輸出d
System.out.println(name.substring(name.length()-1));//輸出d

⑤ java中如何截取字元串中的指定一部分

java用substring函數截取string中一段字元串

在String中有兩個substring()函數,如下:

一:String.substring(intstart)

參數:

start:要截取位置的索引

返回:

從start開始到結束的字元串

例如:Stringstr="helloword!";System.out.println(str.substring(1));

System.out.println(str.substring(3));

System.out.println(str.substring(6));

將得到結果為:

elloword!

loword!

ord!

如果start大於字元串的長度將會拋出越界異常;

二:String.substring(intbeginIndex,intendIndex)

參數:

beginIndex開始位置索引

endIndex結束位置索引

返回:

從beginIndex位置到endIndex位置內的字元串

例如:Stringstr="helloword!";

System.out.println(str.substring(1,4));

System.out.println(str.substring(3,5));

System.out.println(str.substring(0,4));

將得到結果為:

ell

lo

hell

如果startIndex和endIndex其中有越界的將會拋出越界異常。

熱點內容
60編譯 發布:2025-09-14 14:44:14 瀏覽:739
怎麼在我的世界國際版伺服器開創造 發布:2025-09-14 14:44:11 瀏覽:37
解壓拓展怎麼選 發布:2025-09-14 14:28:00 瀏覽:221
手機無線存儲器 發布:2025-09-14 14:18:24 瀏覽:977
將編程納入 發布:2025-09-14 14:08:20 瀏覽:402
android進度條使用 發布:2025-09-14 14:01:06 瀏覽:854
怎麼看伺服器哪些埠沒有被佔用 發布:2025-09-14 14:01:06 瀏覽:229
星際方塊伺服器家園世界如何禁足 發布:2025-09-14 14:00:52 瀏覽:345
我的世界如何創建一個有模組的伺服器 發布:2025-09-14 13:42:40 瀏覽:330
安防場景如何選擇伺服器 發布:2025-09-14 13:20:48 瀏覽:741