簡體   English   中英

查找字符串中字符的出現

[英]Find the occurence of a character in a string

有人可以告訴我如何找到字符串中字符的出現嗎? 我必須檢查在動態創建的div中顯示的行數(使用br標簽)。 在此先感謝,Ashriya

你可以試試

JavaScript indexOf()方法

或者使用splitarray length

您可以使用split並獲取數組的長度:

var div = document.getElementById('div');
alert(div.innerHTML.split(/\<br\s*\/\>/).length;

/\\<br\\s*\\/*\\>/是查找所有<br/><br />的正則表達式

string.indexOf('c')

正如@astander所建議的,SPLIT可能是一個不錯的選擇

您也可以使用正則表達式

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class javaRE { 
    public static void main(String[] args) { 

    String message = "Your contents";

    Pattern p = Pattern.compile("your character");
    Matcher matcher = p.matcher(message); 

    int count=0;
    while(matcher.find()&& matcher.group() != null){
        count++;
    }
    } 
}

上面的代碼將幫助您了解子字符串或其他模式的任何字符的出現。

最好的辦法

string.lastIndexOf('a');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM