Java usage drops

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.