Java101
  • Welcome
  • Videos 🖥️
    • Video Links
  • Exercises
    • Assignment 1 - Tests - הבודק
    • Exercise 2
    • Exercise 4
  • Lesson 1 - Intro
    • MyConsole
    • Variables, Conditionals and Exercises
    • More Exercises
    • IDE Shortcuts
  • Lesson 2
    • Lesson 2 - While
  • Lesson 3 - for loops
    • for loops
  • Lesson 4 - Arrays
    • Arrays
  • Lesson 5 - Functions and Multi-dim Arrays
    • Multidimensional arrays & Functions
  • Lesson 6 - Strings, JUnit and Recursion
    • Useful String functions and more
    • Strings
    • More Exercises
  • Lesson 7
    • Recursion
    • More Exercises
  • Lesson 8 - OOP
    • Intro to Classes and Objects
    • Exercise: Creating circles
    • Lesson 9 - More OOP
  • Lesson 9 - Linked Lists
    • Linked Lists
  • Lesson 10 - Interfaces
    • Interface - Intro
    • Interfaces - Exercises
  • Lesson 11 - Test Review
    • index
Powered by GitBook
On this page
  • Java
  • StringBuilder
  • equals()
  • syntax
  • example
  • compareTO()
  • join()
  • split()
  • more useful functions

Was this helpful?

  1. Lesson 6 - Strings, JUnit and Recursion

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()
PreviousMultidimensional arrays & FunctionsNextStrings

Last updated 4 years ago

Was this helpful?