startsWith method / endsWith method, split method, append method

I am a beginner, but I summarized it for personal study

startsWith method / endsWith method

-A method to check whether the character string starts (ends) with the character specified by the argument. -The result of the check is returned as ** boolean value **

split method

-Method for ** splitting ** a string -Specify the part to be divided with ** regular expression **

append method

-Method of ** StringBuilder class ** that adds a new string to the string -Used when dynamically constructing strings using string literals and variable values ** Literal ** ・ ・ ・ ** Value ** assigned to a variable in the source code · Overloaded and can accept various types of arguments ① ** Primitive type ** (all 8 types) ** Primitive type ** ・ ・ ・ boolean, char, byte, short, int, long, float, double  ②String ③ ** char array ** ④ ** Object ** Such

-Convert all primitive types to character strings. For example, a true / false literal of true becomes a string of "true" -String and char arrays can also receive ranges Example) Specify the range of String or char array

Sample.java


public class Sample {
	public static void main(String[] args) {

		StringBuilder sb = new StringBuilder();
		sb.append("abcde", 1, 3);
		System.out.println(sb);//Execution result:bc

		StringBuilder sb2 = new StringBuilder();
		char[] array = { 'a', 'b', 'c', 'd', 'e' };
		sb2.append(array, 1, 3);
		System.out.println(sb2);//Execution result:bcd
	}
}

-When adding an object, the append method calls the ** toString method ** of that object and adds the string representation of the object to the string. Example) ** Override ** of toString method

Sample.java


class Sample {
		@Override
		public String toString() {
			return "hello";
		}
}

Example) Adding a character string using the append method

Sample.java


		StringBuilder sb=new StringBuilder();
		sb.append(new Sample());//Added the result of the toString method

Recommended Posts

startsWith method / endsWith method, split method, append method
[Java] split method
[Ruby basics] split method and to_s method