java字元串中查找字元
⑴ java如何實現在一個字元串中查找另一個字元串
要判斷boy是不是後者中的一部分,不用循環,只要用String類的indexOf函數就行了。
代碼如下:
public
class
HH
{
public
static
void
main(String[]
args)
{
String
s="he
is
a
boy";
int
result=s.indexOf("boy");
if(result>=0){
System.out.println("boy是he
is
a
boy的一部分");
}else{
System.out.println("boy不是he
is
a
boy的一部分");
}
}
}
運行結果:
boy是he
is
a
boy的一部分
⑵ 簡單的java字元串中查找字元問題
代碼如下:
package demo.joshua;
public class MyFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
filterStr("我喜歡Java程序設計");
filterStr("漢語(Chinese)是我們的母語");
}
public static void filterStr(String str) {
Integer sum = 0;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < 171948 && str.charAt(i) > 19968) {
sb.append(str.charAt(i));
sum++;
}
}
System.out.println("字元串中包含的漢字:" + sb.toString());
System.out.println("漢字的個數:" + sum);
}
}
⑶ java 怎麼獲得字元串中某一字元的位置
在java中使用indexOf方法即可獲得字元串中某一字元的位置,例如Stringstr="abcdef",System.out.println(str.indexOf("c"))。
⑷ java中如何能查詢出字元串中某個字母的位置
str為你要測試的字元串第一種方法:byte[]temp=str.getbytes();//使用平台默認的字元集將此string解碼為位元組序列,並將結果存儲到一個新的位元組數組中。intcount=0;//遍歷數組的每一個元素,也就是字元串中的每一個字母for(inti=0;i<temp.length;i++){//如果字母等於cif(temp[i].equals('c')){//計數器加一count++;}}第二種:intcount=0;stringstr=//你要測試的字元串//index為字元串中第一次出現c的位置,如果字元串中沒有c將返回-1intindex=str.indexof(c);//如果字元串中有cwhile(str.indexof(c)!=-1){count++;//將字元串出現c的位置之前的全部截取掉str=str.substring(str.indexof(c));}考慮大小寫:str=str.tolowercase();//將字元串全部轉化成小寫
⑸ JAVA中如何查找字元串
問題很簡單:
1.首先,你的數據源是數組,那麼要想對每一個值作操作,必須遍歷,所以就有如下代碼:
for(int i=0;i<A.length;i++){
...
}
2.在循環當中,數組中的每一個正在遍歷的元素,就是:A[i];
3.String是java的一個字元串處理類,他有一個非常好用的方法就是,
public boolean startsWith(String prefix),測試此字元串是否以指定的前綴開始。 所以:A[i].startsWith("C")如果返回true,那麼他就是以C開頭的。
4.綜上所述,實現很簡單,完成代碼如下:
public class Test{
public static void main(String[] args){
String[] A ={"CSDF","FDS","CFDSA","DFS","FET"};
for(int i=0;i<A.length;i++){
if(A[i].startsWith("C")){
System.out.println(A[i]);
}
}
}
}
總結:
臨時寫的,代碼沒有經過測試,但敢保證其正確性的幾率很大,祝你成功。
⑹ java中如何查找字元串中的'\'
可以用正則表達式.
如果要表示java源碼中的正則表達式的一個正常的'\'字元,則需要這樣表示'\\\\',其中第一第三個'\'均表示java編譯器中的轉義字元第二個則表示正則表達式中的轉義字元,從而把第四個'\'轉義為正則表達式中一個普通的'\'字元
⑺ JAVA中怎樣在一個字元串中查找給定的子字元串
調用類java.lang.String
的以下方法都可以:
public int indexOf(String str)
返回指定子字元串在此字元串中第一次出現處的索引。
參數:
str - 任意字元串。
返回:
如果字元串參數作為一個子字元串在此對象中出現,則返回第一個這種子字元串的第一個字元的索引;如果它不作為一個子字元串出現,則返回 -1。
public int indexOf(String str,int fromIndex)
返回指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。
參數:
str - 要搜索的子字元串。
fromIndex - 開始搜索的索引位置。
返回:
指定子字元串在此字元串中第一次出現處的索引,從指定的索引開始。
public int lastIndexOf(String str)
返回指定子字元串在此字元串中最右邊出現處的索引。將最右邊的空字元串 "" 視為出現在索引值 this.length() 處。
參數:
str - 要搜索的子字元串。
返回:
如果字元串參數作為一個子字元串在此對象中出現一次或多次,則返回最後一個這種子字元串的第一個字元。如果它不作為一個子字元串出現,則返回 -1。
public int lastIndexOf(String str,int fromIndex)
返回指定子字元串在此字元串中最後一次出現處的索引,從指定的索引開始反向搜索。
參數:
str - 要搜索的子字元串。
fromIndex - 開始搜索的索引位置。
返回:
指定子字元串在此字元串中最後一次出現處的索引。
⑻ java 如何查找匹配的字元和字元串
你可以自己寫個方法的!
從返回的第一個位置開始substring,同時記住位置。
public int[] getOffset(String str,String s){
int[] arr=new int[str.length];
int j=1;
while(str.indexOf(s)!=-1){
int i=str.indexOf(s);
if(j==1){
arr[j-1]=i;
}else{
arr[j-1]=i+arr[j-2]+1;
}
String st=str.substring(i+1);
System.out.println(st);
str=st;
j++;
System.out.println("j="+j);
}
return arr;
}
public static void main(String[] args) {
String str="abcaabbddab";
StringText st=new StringText();
int[] i=st.getOffset(str, "ab");
for(int j:i){
System.out.println(j);
}
}
⑼ java編程字元串查找
importjava.util.*;
publicclassTester{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
while(true){
Stringpass=sc.nextLine();
if(pass.matches("^.{0,5}$")){
System.err.println("NOPasswordatleast6digits");
}elseif(pass.matches("^.*[^a-zA-Z\d].*$")){
System.err.println("");
}elseif(pass.matches("^[a-zA-Z]{6,}$")){
System.err.println("NOPasswordisatleastonenumber");
}else{
System.out.println("YES");
break;
}
}
sc.close();
}
}