當前位置:首頁 » 編程語言 » 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其中有越界的將會拋出越界異常。

熱點內容
app什麼情況下找不到伺服器 發布:2025-05-12 15:46:25 瀏覽:714
php跳過if 發布:2025-05-12 15:34:29 瀏覽:467
不定時演算法 發布:2025-05-12 15:30:16 瀏覽:131
c語言延時1ms程序 發布:2025-05-12 15:01:30 瀏覽:166
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:736
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:148
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:398
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:544
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:631
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:365