Common methods of Java foundation _0308:string class

Source: Internet
Author: User

Remove the character of the specified index--using the Charat () method
public class StringDemo {    public static void main(String args[]) {        String str = "hello";       // 定义字符串对象        char c = str.charAt(0);     // 截取第一个字符        System.out.println(c);      // 输出字符    }}程序执行结果:h
Conversion of character arrays to strings
public class StringDemo {    public static void main(String args[]) {        String str = "hello";       // 定义字符串        char[] data = str.toCharArray(); // 将字符串变为字符数组        for (int x = 0; x < data.length; x++) { // 循环输出每一个字符            System.out.print(data[x] + "、");        }    }}程序执行结果: h、e、l、l、o、
Turn a string to uppercase
public class StringDemo {    public static void main(String args[]) {        String str = "hello“;       // 字符串由小写字母组成        char[] data = str.toCharArray();    // 将字符串变为字符数组        for (int x = 0; x < data.length; x++) { // 改变每一个字符的编码值            data[x] -= 32;        }        System.out.println(new String(data));   // 将全部字符数组变为String        System.out.println(new String(data, 1, 2)); // 将部分字符数组变为String    }}程序执行结果:HELLOEL
Given a string, it is required to determine whether it is made up of numbers.
public class StringDemo {    public static void main(String args[]) {        String str = "123423432";        if (isNumber(str)) {            System.out.println("字符串由数字组成!");        } else {            System.out.println("字符串由非数字组成!");        }    }    public static boolean isNumber(String temp) {        char[] data = temp.toCharArray();// 将字符串变为字符数组,可以取出每一位字符进行判断        for (int x = 0; x < data.length; x++) {// 循环判断            if (data[x] > ‘9‘ || data[x] < ‘0‘) {// 不是数字字符范围                return false;// 后续不再判断            }        }        return true;    // 如果全部验证通过返回true    }}程序执行结果: 字符串由数字组成!
To observe the conversion of a string to a byte array
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";  // 定义字符串        byte[] data = str.getBytes();   // 将字符串变为字节数组        for (int x = 0; x < data.length; x++) {            data[x] -= 32;  // 将小写字母变为大写形式        }        System.out.println(new String(data)); // 全部转换        System.out.println(new String(data, 5, 5));// 部分转换    }}程序执行结果:HELLOWORLD(“new String(data)”语句执行结果)WORLD(“new String(data, 5, 5)”语句执行结果)
Equal judgment
public class StringDemo {    public static void main(String args[]) {        String stra = "Hello"; // 实例化字符串对象        String strb = "hELLO"; // 实例化字符串对象        System.out.println(stra.equals(strb));  // 比较结果:false        System.out.println(stra.equalsIgnoreCase(strb));  // 比较结果:true    }}程序执行结果:false(“stra.equals(strb)”语句执行结果)true(“stra.equalsIgnoreCase(strb)”语句执行结果)
Observe the CompareTo () method
public class StringDemo {    public static void main(String args[]) {        String stra = "Hello";  // 定义字符串对象        String strb = "HEllo";  // 定义字符串对象        System.out.println(stra.compareTo(strb)); // 32,大于0        if (stra.compareTo(strb) > 0) {// 可以利用大小等于0的方式来判断大小            System.out.println("大于");        }    }}程序执行结果:32(“stra.compareTo(strb)”语句执行结果,比较的是两个字符串的编码数值)大于(“System.out.println("大于");”语句执行结果)
Use features such as indexof () to find
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 实例化字符串对象        System.out.println(str.indexOf("world"));   // 返回满足条件单词的第一个字母的索引        System.out.println(str.indexOf("l"));   // 返回的是第一个查找到的子字符串位置        System.out.println(str.indexOf("l", 5));    // 从第6个元素开始查找子字符串位置        System.out.println(str.lastIndexOf("l"));   // 从后开始查找指定字符串的位置    }}程序执行结果:5(“System.out.println(str.indexOf("world"));”语句执行结果)2(“System.out.println(str.indexOf("l"));”语句执行结果)8(“System.out.println(str.indexOf("l", 5));”语句执行结果)8(“System.out.println(str.lastIndexOf("l"));”语句执行结果)
Use the IndexOf () method to determine if the substring exists
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld“;  // 字符串对象        if (str.indexOf("world") != -1) { // 能找到子字符串            System.out.println("可以查询到数据。");        }    }}程序执行结果:可以查询到数据。
Use the contains () method to determine if the substring exists
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld“;      // 字符串对象        if (str.contains("world")) {    // 子字符串存在            System.out.println("可以查询到数据。");        }    }}程序执行结果: 可以查询到数据。
Start or end judgment
public class StringDemo {    public static void main(String args[]) {        String str = "##@@hello**";     // 字符串对象        System.out.println(str.startsWith("##"));// 是否以“##”开头        System.out.println(str.startsWith("@@", 2));    // 从第2个索引开始是否以“@@”开头        System.out.println(str.endsWith("**"));// 是否以“**”结尾    }}程序执行结果:true(“str.startsWith("##")”语句执行结果)true(“str.startsWith("@@", 2)”语句执行结果)true(“str.endsWith("**")”语句执行结果)
Example: Observing the result of the substitution
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld" ;         // 定义字符串        String resultA = str.replaceAll("l","_") ;  // 全部替换        String resultB = str.replaceFirst("l","_") ;    // 替换首个        System.out.println(resultA) ;        System.out.println(resultB) ;    }}程序执行结果:he__owor_d(“System.out.println(resultA)”语句执行结果)he_loworld(“System.out.println(resultB)”语句执行结果)
Validating string Operations
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 定义字符串        String resultA = str.substring(5);  // 从指定索引截取到结尾        String resultB = str.substring(0, 5);   // 截取部分子字符串        System.out.println(resultA);        System.out.println(resultB);    }}程序执行结果:world(“System.out.println(resultA)”语句执行结果)hello(“System.out.println(resultB)”语句执行结果)
Do all of the splitting
public class StringDemo {    public static void main(String args[]) {        String str = "hello yootk nihao mldn";// 定义字符串,中间使用空格作为间隔        String result[] = str.split(" ");   // 字符串拆分        for (int x = 0; x < result.length; x++) {   // 循环输出            System.out.print(result[x] + "、");        }    }}程序执行结果:hello、yootk、nihao、mldn、
Split into a specified number
public class StringDemo {    public static void main(String args[]) {        String str = "hello yootk nihao mldn";  // 定义字符串,中间使用空格作为间隔        String result[] = str.split(" ",2);     // 字符串拆分        for (int x = 0; x < result.length; x++) {   // 循环输出            System.out.println(result[x]);        }    }}程序执行结果:helloyootk nihao mldn
Complex split
public class StringDemo {    public static void main(String args[]) {        String str = "张三:20|李四:21|王五:22“;// 定义字符串        String result[] = str.split("\\|"); // 第一次拆分        for (int x = 0; x < result.length; x++) {            String temp[] = result[x].split(":");   // 第二次拆分            System.out.println("姓名:" + temp[0] + ",年龄:" + temp[1]);        }    }}程序执行结果:姓名:张三,年龄:20姓名:李四,年龄:21姓名:王五,年龄:22
String connection
public class StringDemo {    public static void main(String args[]) {        String str = "hello".concat("world") ;  // 等价于“+”        System.out.println(str) ;    }}程序执行结果:helloworld
Example: Turn lowercase and uppercase operations
public class StringDemo {    public static void main(String args[]) {        String str = "(*(*Hello(*(*" ;      // 定义字符串        System.out.println(str.toUpperCase()) ; // 转大写后输出        System.out.println(str.toLowerCase()) ; // 转小写后输出    }}程序执行结果:(*(*HELLO(*(*(“System.out.println(str.toUpperCase())”语句执行结果)(*(*hello(*(*(“System.out.println(str.toLowerCase())”语句执行结果)
Remove left and right spaces
public class StringDemo {    public static void main(String args[]) {        String str = "   hello   world  ";  // 定义字符串,包含空格        System.out.println("【" + str + "】");// 原始字符串        System.out.println("【" + str.trim() + "】");// 去掉空格后的字符串,中间的空格保留    }}程序执行结果:【   hello   world  】【hello   world】
Get string length
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";  // 定义字符串        System.out.println(str.length());// 取得字符串长度    }}程序执行结果:10
Example: determining whether an empty string
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 定义字符串        System.out.println(str.isEmpty());  // 判断字符串对象的内容是否为空字符串(不是null)        System.out.println("".isEmpty());    // 判断字符串常量的内容是否为空字符串(不是null)    }}程序执行结果:false(“System.out.println(str.isEmpty())”语句执行结果)true(“System.out.println("".isEmpty())”语句执行结果)
To implement the first letter capitalization operation
public class StringDemo {    public static void main(String args[]) {        String str = "yootk";   // 定义字符串        System.out.println(initcap(str));// 调用initcap()方法    }    /**     * 实现首字母大写的操作     * @param temp 要转换的字符串数据     * @return 将首字母大写后返回     */    public static String initcap(String temp) {        // 利用substring(0,1)取出字符串的第一位而后将其大写,再连接剩余的字符串        return temp.substring(0, 1).toUpperCase() + temp.substring(1);    }}程序执行结果:Yootk

Common methods of Java foundation _0308:string class

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.