• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Methods I in Java

April 8, 2022 Java

In this post, you will learn how to work with two specific methods (substring() and split()) so that you can use that same knowledge and thus evolve.

substring() Returns a substring (a new string with characters from a certain range) according to the start index and the end index
split() Divides a set of characters (according to the given parameter/argument) and returns it into subsets of characters, that is, returns an array of strings (Regex is referring to the use of regular expressions).
package com.caffeinealgorithm.programminginjava;

public class MethodsI {
  private String oneTwoThree = "OneTwoThree", name = "Nelson Gomes da Silva";

  public void Run() {
    // substring()
    System.out.printf("1: %s\n", oneTwoThree.substring(0, 2));
    System.out.printf("2: %s\n", oneTwoThree.substring(2, 6));
    System.out.printf("3: %s\n", oneTwoThree.substring(6));

    // split()
    var words = name.split(" ");

    for (String word : words)
      System.out.printf("Word: %s\n", word);

    var _words = name.split(" Gomes da ");
    System.out.printf("Name: %s %s", _words[0], _words[1]);
  }
}

/*
  1: One
  2: Two
  3: Three
  Word: Nelson
  Word: Gomes
  Word: da
  Word: Silva
  Name: Nelson Silva
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved