JAVA Study Notes --------- String API

JAVA Study Notes --------- String API

 

1 determines whether the end of a character string to another string

 

/**

 * Determining whether a string ends with another string

 */

 

private static void endsWithPrint() {

String str="howareyou";

boolean b=str.endsWith("ou");

System.out.println(b);

}

 

 

2 determines whether a string contains another string

/**

 * Determines whether a string contains another string

 *

 */

private static void containStr() {

String str="abc";

boolean b=str.contains("a");

System.out.println(b);

}

3. Finds the index of a character in a string of the first occurrence

/**

 * Find index of a character in a string of the first occurrence

 * int indexof(char ch)

 */

 

private  static  void  indexOfPrint () {

String str="abcdefg";

you  i=str.indexOf (f ');

. The System OUT .println ( "F appears in the first index position in the string:" + I );

}

  1. Converts a string to a byte array

/**

 * Converts a string to a byte array

 * byte getBytes()

 */

private static void getBytesPrint() {

String str="abc";

byte bytes[]= str.getBytes();

for (int i = 0; i < bytes.length; i++) {

System.out.println(bytes[i]);

 

}

5. Analyzing the content of the two strings are the same, ignoring case

 

/**

 * boolean equals(Object obj)

 Passing the resulting string *, it is determined whether the character string identical, returns true if identical

 *

 *

 * boolean equalsIgnoreCase(String s)

 * Determine the contents of two strings are the same, ignoring case

 */

 

private static void equalsIgnoreCaseDemo() {

String str1="abc";

String str2="ABC";

System.out.println(str1);

System.out.println(str2);

boolean b1=str1.equals(str2);

boolean b2=str2.equalsIgnoreCase(str1);

 

System.out.println("equals案例:"+b1);

System.out.println("equalsIgnoreCase案例:"+b2);

}

  1. The first string of uppercase letters, beginning from the second letter lowercase

/**

 * A string uppercase first letter, the second letter from the beginning to lowercase

 * @Param  Cycle

 */

 

public static void conventChar(String str) {

// Get a first portion of the string

String start=str.substring(0, 1);

 

String end=str.substring(1);

// The first portion converts String uppercase letter lowercase string to the second portion

 

 String big=start.toUpperCase();

 String small=end.toLowerCase();

 . The System OUT .println ( "original string:" + STR );

 . The System OUT .println ( "transformed:" + Big + Small );

 

}

  1.  Large string statistics, the number of lowercase and numeric characters

65 to 90 large write 97 to 122 smaller write numbers 48 to 57

/**

 * Large string statistics, the number of lowercase and numeric characters

 * @Param  Cycle

 */

public static void getCount(String str) {

 

// define three variables

 

int  Upper= 0;

int lower=0;

int  digit= 0;

byte bytes[] =str.getBytes();

for (int i = 0; i < bytes.length; i++) {

 

if(65<=bytes[i]&&bytes[i]<=90) {

upper++;

}

else if (97<=bytes[i]&&bytes[i]<=122) {

lower++;

}else if (48<=bytes[i]&&bytes[i]<=57) {

digit++;

}

 

}

 

. System OUT .println ( 'uppercase characters: " + Upper + " a " );

. System OUT .println ( "lowercase characters:" + Lower + "a" );

. System OUT .println ( "numeric characters:" + digit + "a" );

 

for (int i = 0; i < bytes.length; i++) {

System.out.println(bytes[i]);

}

 

 

Guess you like

Origin blog.csdn.net/lieanwan2780/article/details/88412441