1. Find the position of a character in a string in the first few occurrences
/*** Find the position of a character in the string where it appears in a few times *@paramstring to match with *@paramI the first several times appear *@paramcharacter characters to match *@returnthe location that appears*/ Public Static intGetcharacterposition (String string,inti,string character) { //Matcher slashmatcher = Pattern.compile ("/"). Matcher ("Hahah/hhh/af"); Matcher Slashmatcher =Pattern.compile (character). Matcher (string); intMIDX = 0; //returns 1 if there is no match intResult=-1; while(Slashmatcher.find ()) {Midx++; if(Midx = =i) { //returns the result of the matchresult =Slashmatcher.start (); Break; } } returnresult; }
2. Find the number of occurrences of a character in a string
/*** Find the number of occurrences of a character in a string *@paramSTR String *@paramtoken a character *@returnNumber of occurrences*/ Public Static intCounttoken (String str,string token) {intCount=0; while(Str.indexof (token)!=-1) {Count++; STR= Str.substring (Str.indexof (token) +token.length ()); } returncount; }
Java usage drops