A small case of string class such as the first season of getting Started with Java

Source: Internet
Author: User

The string class has a number of fetch methods, which can be viewed in the API documentation. For the acquisition method, a small case is given.

/* Requirements: traverse to get every character in a string * Analysis: Use two methods:           char charAt (int index) to get the    string specifies the index of the character           int length () to              get the length of the string */ public class Stringtest {public static void main (string[] args) {//define string s = "HelloWorld";               Think, to get each index and more efficient, with a for loop improvement. for (int x = 0; x < s.length (); + +) {//char ch = s.charat (x)///SYSTEM.OUT.PRINTLN (CH)///output only, I directly output System.out.pri NT (S.charat (x));}}}

Then write a small case:

/* Requirements: Counts the number of uppercase characters, lowercase alphabetic characters, and number characters that occur in a string. (regardless of other characters) * Example: * "Hello123world" * Result: * Uppercase characters: 2 * lowercase characters: 8 * Numeric character: 3 *  * Analysis: * Premise: String to exist * A: Define three statistical variables * Bigcount=0 * smallcount=0 * numbercount=0 * B: Iterate through the strings to get each character. * Length () and charat () combine * C: Determine whether the character belongs to that type of * large: bigcount++ * Small: smallcount++ * Number: numbercount++ *d: Output result. * */public class StringTest2 {public static void main (string[] args) {//define string s = "Hello123world";//define three statistical variables int BI Gcount = 0;int Smallcount = 0;int Numbercount = 0;//traverses the string to get each character. for (int x=0; x<s.length (); x + +) {Char ch = s.charat (x);//Determine if the character belongs to that type of if (ch>= ' a ' && ch<= ' z ') { smallcount++;} else if (ch>= ' A ' && ch<= ' Z ') {bigcount++;} else if (ch>= ' 0 ' && ch<= ' 9 ') {numbercount++;}} Output results. System.out.println ("Capital letter" +bigcount+ "one"); SYSTEM.OUT.PRINTLN ("Small Letter" +smallcount+ "one"); System.out.println ("number" +numbercount+ "a");}}

There is actually a simpler way to get the number of uppercase and lowercase numbers. It will be written later.

A small case of string class such as the first season of getting Started with Java

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.