Useful String functions and more

Java

StringBuilder

StringBuilder str = new StringBuilder();

str.append("Hello");
str.append("World");

System.out.println(str.toString());
HelloWorld

equals()

syntax

public boolean equals(Object anotherObject)

example

String myStr1 = "Hello";
String myStr2 = "World";

System.out.println("Hello".toLowerCase().equals("hello")); // Returns true
System.out.println(myStr1.equals(myStr2));                 //Returns false

compareTO()

String myStr1 = "Hello";
String myStr2 = "Hello";
System.out.println(myStr1.compareTo(myStr2)); // Returns 0 because they are equal

System.out.println("be".compareTo("bee"));
0
-1

join()

String date = String.join("/","30","04","2021");    
System.out.print(date);

String time = String.join(":", "12","10","10");  
System.out.println(" "+time);
30/04/2021 12:10:10

split()

String[] strings = "30-04-2021".split("-");

for (int i = 0; i < strings.length; i++)
     System.out.print(strings[i] + " ");
30 04 2021

more useful functions

str.indexOf();
str.substring();
str.replace();
Interger.parseInt();
assertTrue()

Last updated