Home  >  Article  >  Java  >  Usage examples of important methods of java string

Usage examples of important methods of java string

(*-*)浩
(*-*)浩Original
2019-03-30 14:58:482737browse

The String class will be one of your main tools for storing and processing languages. This article will lead you to have a basic understanding of Java string methods and understand how to use its functions.

Usage examples of important methods of java string

1. Return String "length" method

How do you determine the length of a given String? Java provides a method called "length()". Use it for when you need to find the length of a String.

public class Str_Sample {
	public static void main(String[] args){
		//测试String长度的方法
		String name="hello work";
		//length方法返回的是整数
		int num=name.length();
		System.out.println("字符串的长度:"+num);
	}
}

Run the above example, the output is as follows:

字符串长度:10

2. String "indexOf()" method

How can I find which character is in which position?

"indexOf" can help you specify the position where a specific character first appears. If it cannot be found, it returns -1

public class Str_Sample{
    public static void main(String[] args){
       String name="I like java. How do you like java?";
	   int num=name.indexOf("java");
	   System.out.println("java第一次出现的位置:"+num);
    }
}

Run the above example, the output result is as follows:

java第一次出现的位置:7

3. String "lastindexOf()" method

If I know the length, where do I want to find the character from the back of the string?

"lastindexOf" can start the reverse search from the specified position and return the position where the specific character you specified last appears. If it cannot find it, it will return -1

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        //name的长度
        int num=name.length();
	        //lastindexOf(指定特殊字符,指定位置)
              int index=name.lastindexOf("java",num);
                System.out.println("java最后一次出现的位置:"+index);
	     
    }
]

Run the above example, the output result is as follows :

java最后一次出现的位置:29

4. String "substring()" method

What should I do if I only want a section of the string?

"subString" can intercept the string from the specified header and return the intercepted string. Note: The range expressed in Java includes the head but not the tail.

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        //截取How这个字母,首先你要知道H的下标,可使用之前的indexOf方法
        int num=name.indexOf("H");
        //由于java中示范围都是含头不含尾,所以要多加一位
        String str=name.substring(num,num+3);
        System.out.println(str);
        //也可从指定位置直接截取到字符串尾部
        String str2=name.substring(num);
        System.out.println(str2);
    }
]

Run the above example, the output result is as follows:

How
How do you like java?

5. String "charAt()" method

How can I get characters based on position?

"chatAt" can help you, used to return the character of the specified subscript

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        //创建循环遍历name的下标
        for(int i=0;i<name.length();i++){
            //将下标放入charAt方法中
            char ch=name.charAt(i);
            System.out.print(ch);
        }
    }
]

Run the above example, the output result is as follows:

I like java. How do you like java?

6. String "startsWith( ), endsWith()" method

How to determine what the string begins or ends with?

"startsWith()", checks whether the string starts with the specified string. "endsWith()" checks whether the string ends with the specified string

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        //是否以“I”开头,正确返回true,否则返回false
        boolean s1=name.startsWith("I");
        //startsWith()第二种用法,可判断指定位置是否是指定字符串
        boolean s2=name.startsWith("java",7);
        //判断字符串是否以“?”结尾
        boolean e1=name.endsWith("?");
        System.out.println("是否以“I”开头:"+s1);    
        System.out.println("位置7是否是“java”开头:"+s2);
        System.out.println("是否以“?”结尾:"+e1);
      }
}

Run the above example, the output result is as follows:

否以“I”开头:true
位置7是否是“java”开头:true
是否以“?”结尾:true

7. String "compareTo()"Method

"compareTO" It starts the comparison from the first character. If it encounters different characters, it immediately returns the difference in the ascii value of the two characters. The return value is int type .

public class Str_Sample{
    public static void main(String[] args){
        //A的ascli值为65,a的ascli值为97
        String a="A";
        String b="a";
        String c="aa";
        String d="abc";
        String e="ad";
        int num=a.compareTo(b);
        //还有一种方法忽略大小写进行比较
        int num2=a.compareToIgnoreCase(b);
        //长度不一样且前几个字符也不一样,从第一位开始找,当找到不一样的字符时,则返回的值是这两个字符比较的值
        int num3=c.compareTo(d);
        //如多个字符,第一个字符相同则直接比较第二个字符,以此类推
        int num4=e.compareTo(c);
        System.out.println("a与b比较:"+num);
        System.out.println("a与b比较(忽略大小写):"+num2);
        System.out.println("c与d比较:"+num3);
        System.out.println("e与d比较:"+num4);
     }
}

Run the above example, the output result is as follows:

a与b比较:-32
a与b比较(忽略大小写):0
c与d比较:-1
e与d比较:3

8. String "contains()"Method

What if you want to know if a string contains the string you want?

Then "contanins" can meet your needs to determine whether it contains the specified string

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        //判断是否包含“you”这个字符串
        boolean bl=name.contains("you");
        System.out.println("name字符串中是否包含“you”:"+bl);
    }
]

Run the above example, the output results are as follows:

name字符串中是否包含“you”:true

9.字符串“replace()”方法

您可以指定要替换的字符串部分以及参数中的替换字符串。

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like java?";
        String str=name.replace("java", "php");
        System.out.println("替换前:"+name);
        System.out.println("替换后:"+str);
    }
]

运行以上实例,输出结果如下:

替换前:I like java. How do you like java?
替换后:I like php. How do you like php?

10.字符串“toLowerCase()”和“toUpperCase()”方法

“toLowerCase()”将字符串以小写形式显示,toUpperCase()”将字符串以大写形式显示。

public class Str_Sample{
    public static void main(String[] args){
        String name="I like java. How do you like Java?";
        String low=name.toLowerCase();
	    String upp=name.toUpperCase();
        System.out.println("小写显示:"+low);
        System.out.println("大写显示:"+upp);
    }
]

运行以上实例,输出结果如下:

小写显示:i like java. how do you like java?
大写显示:I LIKE JAVA. HOW DO YOU LIKE JAVA?

本篇文章到这里就已经全部结束了,如有不足之处请见谅,更多其他精彩内容可以关注PHP中文网的Java视频教程栏目!

The above is the detailed content of Usage examples of important methods of java string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn