In layman's language java.String

In layman's language java.String

Some common methods Java string handling

Java defines a string

  1. Direct given string

    Defined directly used as the string representation “”to represent the contents of the string

    String str = "Hello Mujey";
    
    String str;
    str = "Hello Mujey"

    Assignment of strings can be declared after the number of type String handle, but must be assignment before using this object.

  2. String class is defined

    String class is in the java.langpackage, when we create a java program, the system will automatically help us references java.lang.*so we can directly use, does not require manualimport

    String str = new String("Hello Mujey");
    String s = new String(str);

    Note: When used as String (), brackets do not enter any parameters, it will create an empty string, and assigned to handle.

    String (), the brackets can also enter an array:

    char[] c = {'M','u','j','e','y'};
    String str = new String(c);
    System.out.println(str); // Mujey

    Into an array in String () when the brackets, the array is essentially a clone operation, so change the set values ​​of the array elements and does not affect the already stored value in the string after the assignment is completed.

    char c = {'h','e','l','l','o'};
    String str = new String(c,0,3);
    System.out.println(str); // hel

    In parentheses you can also pass two integer type parameter for indicating several elements taken from the first few elements, and they are connected into a string.

Conversion between String and Integer

String to int

String type is converted to int type There are two main ways

  • Integer.parseInt(str)
  • Integer.valueOf(str).inValue()
String str = "333";
int i = 0;

// 第一种方法
i = Integer.parseInt(str); // => int: 333
i = Integer.parseInt("345"); // => int: 345

i = 0;
// 第二种方法
i = Integer.valueOf(str).inValue(); // => int: 333
i = Integer.valueOf("345").inValue(); // => int: 345

Int converted to a String

A few days ago in LeetCode do arithmetic problems in time to see a topic to share with you, this problem is very simple. Given a set of array, for example, returns the number of bits is an even number:

Input: [345,365,343,53,43,2532]

Returns: 3

53 and 43 are in double figures, their median 2; 2532 is a four-digit number, its median is 4. So a total of three even-digit number.

The simplest solution to this problem is to type int integer converted to a string, then the string length is determined by the number of bits is an odd or an even number of bits.

 public int findNumbers(int[] nums) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            String s = String.valueOf(nums[i]);
            if (s.length() % 2 == 0) {
                result ++;
            }
        }
        return result;
 }

This is the solution of this question, how, is not very simple. But wherein it is converted to type int relates to a String type of operation.

int type is converted to a String There are three ways:

  • String str = String.valueOf(int)
  • String str = Integer.toString(int)
  • String str = "" + i
int num = 30;

String str1 = String.valueOf(num); // String: "30"
String str2 = Integer.toString(num); // String: "30"
String str3 = "" + num; // String: "30"

Conversion results of these three methods are the same, but bloggers recommend using the previous two ways to convert. Although the introduction last grammar, but the efficiency of its short board is far performed in front of two ways high.

The first embodiment valueOf () must be filled in a value, or will produce a null pointer exception.

Get string length

Careful you must have found in the above LeetCode algorithm problem, I also used the s.length this method, the role of this method is to simply get the length of the string.

In some ways, a bit like a string array of type char, they can be used .lengthto get the length. But slightly different, the length of the string from the .length()back of a pair of parentheses , this description is a method, and an array of char, is a way of obtaining the length of the array of the acquired lengthproperty, so that no one pair of parentheses .

String str = "29493929134";
int i = str.length();
System.out.println(i); // 11

String type of case conversion

String type of case conversion applies only to English, the Chinese are not available, it is taken for granted, because the Chinese not case-sensitive ( '▽ `)

Size English string is accomplished using two methods:

  • Converted to lowercase: str.toLowerCase ()
  • Converted to uppercase: str.toUpperCase ()
String str = "abcd";
System.out.println(str.toUpperCase()); //:=> ABCD
System.out.println(str.toLowerCase()); //:=> abcd

Remove the spaces in the string

If a string of spaces will execute the program: for example, judgment and other operations have an impact, we can use:

str.trim()This method to remove the spaces in the string:

String str = "Hello Java";
System.out.println(str.trim()); //:=>HelloJava

Note : .trim()The method can only be removed en space string, in other words full-width space, using the Chinese state under .trim()the method can not be removed.

We need to use this .replace()method, the full-size space string transfer into half-size space, and then use .trim()the method to remove it, .replace()the method requires two parameters when in use, is replaced by the replacement character and the character thereof.

unicode encoding full-size space is 12288, so I want to talk about em space, en space is replaced, you only need to

str.replace((char)12288, ' ');

Split string interception and

Interception string

substring () method can be used to intercept the string, this method can pass up to two parameters, a minimum.

In a parameter passed from the time it means that the first few characters of the string to start capturing the last character.

And when this method receives two parameters, and the second parameter must be greater than the first parameter, the method returns a string from the first segment to the first few characters of several character.

String str = "I like programming";
System.out.println(str.substring(2)); //:=>like programming
System.out.println(str.substring(1)); //:=> like programming
System.out.println(str.substring(2,5)); //:=>like

Split string

split () method can be used to separate the strings

split () method can wear a String type of split flag, which is essential, .and |these two characters need to need to use the \\escape character to escape, or are unable to recognize their java

String str = "Java,Python,Go,C#,Swift,Ruby,PHP,Object-C";
String[] strs = str.split(",");
for (str : strs){ System.out.print(str + "\t"); }
//  Java    Python  Go  C#  Swift   Ruby    PHP Object-C

String frontEnd = "JavaScript|HTML5|CSS|jQuery|vue|React|Bootstrap";
String[] frontEnds = frontEnd.split("\\|"); // 使用转义字符\\
for (str : frontEnds){ System.out.print(str + "\t");}
//  JavaScript  HTML5   CSS jQuery  vue React   Bootstrap

In this latter type of parameter string, you can also add a parameter of type int

String str = "Java,Python,Go,C#,Swift,Ruby,PHP,Object-C";
String[] strs = str.split(",", 4);
for (str : strs){ System.out.print(str + "\t"); }
//  Java    Python  Go  C#,Swift,Ruby,PHP,Object-C

Guess you like

Origin www.cnblogs.com/mujey/p/12233276.html