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

熱點內容
易網頁源碼 發布:2024-04-27 04:51:06 瀏覽:864
攜程伺服器是什麼牌子 發布:2024-04-27 04:31:50 瀏覽:745
醫院新冠肺炎疫情防控演練腳本 發布:2024-04-27 04:04:45 瀏覽:652
天津智慧網關伺服器雲伺服器 發布:2024-04-27 03:56:51 瀏覽:422
移門製作下料尺寸演算法 發布:2024-04-27 03:15:02 瀏覽:641
c語言5常量 發布:2024-04-27 02:38:49 瀏覽:991
源碼怎麼搭建 發布:2024-04-27 02:33:44 瀏覽:97
java獲取參數 發布:2024-04-27 02:22:21 瀏覽:501
unixlinuxwindows 發布:2024-04-27 02:10:55 瀏覽:445
nginx禁止ip訪問網站 發布:2024-04-27 02:05:43 瀏覽:845