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

熱點內容
linux如何共享文件夾 發布:2023-03-27 12:02:55 瀏覽:521
ubuntuserver如何配置網路 發布:2023-03-27 12:02:23 瀏覽:320
動態伺服器頁面縮寫是什麼 發布:2023-03-27 12:02:20 瀏覽:444
linuxtftp上傳 發布:2023-03-27 12:02:15 瀏覽:604
右鍵壓縮選項 發布:2023-03-27 12:01:32 瀏覽:598
java泛型t 發布:2023-03-27 11:54:24 瀏覽:922
魔域在哪裡可以修改倉庫密碼 發布:2023-03-27 11:51:04 瀏覽:160
行李箱加密 發布:2023-03-27 11:49:39 瀏覽:39
試演算法咋用 發布:2023-03-27 11:42:37 瀏覽:328
eclipse編譯成jar 發布:2023-03-27 11:41:23 瀏覽:67