Java Strings: Basic String Methods Part 1

Java Strings: Basic String Methods Part 1

Learn the most common Java String Methods.

Introduction

In this short tutorial, we'll go through some of the common Java String methods that you well use daily.

1. Get the String's Length

Getting the length of a string is an ordinary operation that developers perform on a daily bases.

To get the length of a string, we use the length(). Below is an example of getting the length of a string.

Here is an example of getting the length of a blank string:

@Test
void stringLengthEqualToZero() {
  // Test a string with a length 0.
  // Test will return true.
  assertEquals(0, "".length());
}

Here is an example of getting the length of a string greater than zero:

@Test
void stringLengthGreaterThanZero() {
  // Test a string with a length of 4.
  // Test will true.
  assertEquals(4, "test".length());
}

2. Trimming a String

Sometimes you need to remove the whitespaces at the beginning of a string or the ending of a string. To do this, we use the trim() method. The trim method removes all whitespaces from the beginning and end of a string.

Here is an example of trimming the beginning of a string:

@Test
void stringTrimBeginning() {
  // Trim the beginning of a string.
  assertEquals("test", " test".trim());
}

Here is an example of trimming the end of a string:

@Test
void stringTrimEnd() {
  // Trim the end of a string.
  assertEquals("test", "test ".trim());
}

Here is an example of trimming a whole string:

@Test
void stringTrimWhole() {
  // Trim the end of a string.
  assertEquals("test", " test ".trim());
}

3. Does String Start With

To test if a string starts with a sub-string we use the startsWith() method.

Here is a example of testing is a string beginnings with sub-string:

@Test
void stringBeginningsWith() {
  // Test if a string beginnings with.
  // Test will return true.
  assertTrue("pre-test".startsWith("pre"));
}

Here is a example with testing if a string beginnings with sub-string start at an index:

@Test
void stringBeginningsWithAtIndex() {
  // Test if a string beginnings with at index.
  // Test will return true.
  assertTrue("pre-test".startsWith("test",4));
}

4. Does String End With

To test is a string ends with a sub-string we use the endsWith() method.

Here is a code example of testing if a string ends with a sub-string:

@Test
void stringEndWith() {
  // Test if a string ends with.
  // Test will return true.
  assertTrue("test".endsWith("st"));
}

Conclusion

In this short tutorial, you learned how to use the length, trim, startsWith and endsWith Java String methods.

Did you like it? Why don't you try also...

Split Strings in Java

Split Strings in Java

Learn how to split strings in Java

Java Strings: Testing if a String is Empty or Blank

Java Strings: Testing if a String is Empty or Blank

Learn to test if a String is Empty or Blank

Java String: Title Case Format

Java String: Title Case Format

Learn How to Convert Java String to Title Case Format