• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Methods I in C#

January 26, 2022 C#

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 length, the length being optional
Split() Divides a set of characters (according to a given parameter/argument) and returns it into subsets of characters, that is, returns an array of strings (Class Regex refers to the use of regular expressions)
using System;
using System.Text.RegularExpressions;

namespace Base {
  class MethodsI {
    private string oneTwoThree = "OneTwoThree", name = "Nelson Gomes da Silva";

    public void Run() {
      // Substring()
      Console.WriteLine($"1: {oneTwoThree.Substring(0, 2)}");
      Console.WriteLine($"2: {oneTwoThree.Substring(2, 4)}");
      Console.WriteLine($"3: {oneTwoThree.Substring(6)}");

      // Split()
      var words = name.Split(' ');

      foreach (var word in words)
        Console.WriteLine($"Word: {word}");

      var _words = Regex.Split(name, " Gomes da ");
      Console.WriteLine($"Name: {_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