String operations this and that-Java edition-

String operations this and that-Java edition-

For the time being, it is summarized as a memorandum. It may not be practical. I will write from Java for the time being. Please point out if you write something strange.

String concatenation

Sample1.java


/**Combine character literals and constants**/
String final CONSTSTR = "A";
String strA = "A" + "B" + CONSTSTR; //Literals and constants+Good to combine with
System.out.println(strA); // ABA

/**Other joins**/
String strB = "B";
String strC = "C";
StringBuilder buff = new StringBuilder();
buff.append(strA);
buff.append(strB);
buff.append(strC);

This article is detailed https://qiita.com/yoshi389111/items/67354ba33f9271ef2c68

Convert strings to numbers

Sample2.java


/**Convert from string to number normally**/
String strA = "1";
int numA = Integer.parseInt(strA);

/**Considering exception handling, 0 is returned at the time of exception for the time being**/
String hoge = "hoge";
try {
    int numB = Integer.parseInt(hoge);
} catch (NumberFormatException e) {
    return 0;
}

Cut out a character string (specify a range)

Sample3.java


/**Cut out by specifying the start point and end point to cut out**/
String strA = "12345";
strA.substring(0,3); // 123
strA.substring(1,3); // 23

/**Cut out by specifying only the starting point to cut out**/
strA.substring(2); // 345

Cut out only one character string

Sample4.java


/**Cut out only one character with Char type**/
String strA = "12345";
Char charA = strA.charAt(3); // 4

Remove leading and trailing whitespace

Sample5.java


/**White space before and after(Half-width only)Delete**/
String strA = " ABC ";
strA.trim(); // ABC

Replace string

Sample6.java


String strA = "ABCABCABC";
/**Replace all A with X**/
strA.replace('A', 'X'); // XBCXBCXBC
/**Replace only the very first A with X**/
strA.replaceFirst('A', 'X'); // XBCABCABC

Exact match judgment of character string

Sample7.java


String strA = "ABC";
String strB = null;

/**Determine if it is an exact match**/
"ABC".equals(strA); // true
"CBA".equals(strA); // false
/**If the character string to be judged is null, it is judged as false.**/
"ABC".equals(strB); // false

Match judgment of character string ignoring case

Sample8.java


String strA = "ABC";
String strB = "xyz";

/**Ignore case and determine if they match**/
"abc".equalsIgnoreCase(strA); // true
"XYZ".equalsIgnoreCase(strB); // true

Judge whether the character string is a prefix match or a suffix match

Sample9.java


String strA = "ABCDE";

/**Determine if it is a prefix match or a suffix match**/
strA.startsWith("AB"); // true
strA.endWith("DE"); // true

Determine if the string is empty

Sample10.java


String strA = "";

/**Determine if the string is empty**/
strA.isEmpty(); // true

Determine if a specific string is included

Sample11.java


String strA = "ABCDEFGHIJ";

/**Determine if a specific character string is included**/
strA.contains("DE"); // true

Recommended Posts

String operations this and that-Java edition-
String and stringbuffer and string builder
String literals and instances
[Java] String comparison and && and ||
Various Ruby string operations
Basic operators and operations
[Java] About String and StringBuilder
String concatenation and line breaks
Digital certificate this and that
Base64 encoder this and that