• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Methods II in C#

January 27, 2022 C#

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

IndexOf() Returns a substring from the first occurrence of a character (or even a set of characters) to the end of the given string
Trim() Eliminates spaces present at the beginning and end of a string, that is, returns a modified copy of the original string
using System;

namespace Base {
  class MethodsII {
    private string loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur.";

    public void Run() {
      // IndexOf()
      var index = 0;

      while ((index = loremIpsum.IndexOf('i', index)) != -1) {
        Console.WriteLine(loremIpsum.Substring(index));
        index++;
      }

      // Trim()
      Console.Write("Enter your first name: ");
      var firstName = Console.ReadLine();

      Console.Write("Enter your last name: ");
      var lastName = Console.ReadLine();

      Console.WriteLine($"Name (without using the Trim() method): {firstName} {lastName}");
      Console.WriteLine($"Name (using the Trim() method): {firstName.Trim()} {lastName.Trim()}");
    }
  }
}

/*
  ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur.
  it amet, consectetur adipiscing elit. Vestibulum consectetur.
  ipiscing elit. Vestibulum consectetur.
  iscing elit. Vestibulum consectetur.
  ing elit. Vestibulum consectetur.
  it. Vestibulum consectetur.
  ibulum consectetur.
  Enter your first name:          Nelson
  Enter your last name:    Silva
  Name (without using the Trim() method):          Nelson          Silva
  Name (using the Trim() method): 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