How to remove First and Last character from String in Scala? Last Updated : 16 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will explore different approaches to removing the first and last character from a string in Scala. Table of Content Using substring methodUsing drop and dropRight methodsUsing substring methodIn this approach, we are using the substring method which takes two arguments the starting index and the ending index. By passing 1 as the starting index and str.length - 1 as the ending index, we can remove the first and last characters from the string. Syntax: str.substring(startIndex: Int, endIndex: Int) Example: Scala def removeFirstAndLast(str: String): String = { str.substring(1, str.length - 1) } // Example usage: val originalString = "GeeksForGeeks" val modifiedString = removeFirstAndLast(originalString) println(modifiedString) OutputeeksForGeekUsing drop and dropRight methodsIn this approach we are using drop and dropRight method. drop method is used to remove first n elements from the collection and dropRight removes the last n elements. By calling drop(1) and dropRight(1) on the string, we remove the first and last characters, respectively. Syntax: str.drop(1).dropRight(1) Example: Scala def removeFirstAndLast(str: String): String = { str.drop(1).dropRight(1) } // Example usage: val originalString = "Welcome to GFG" val modifiedString = removeFirstAndLast(originalString) println(modifiedString) Outputelcome to GF Comment More infoAdvertise with us Next Article How to remove duplicate characters from a String in Scala? Y yuvraj07 Follow Improve Article Tags : Scala Similar Reads How to remove duplicate characters from a String in Scala? In this article, we will learn how to remove duplicate characters from a string in Scala using different approaches. First, we will look through the Brute-Force Approach and then use different standard libraries. Examples: Input: String = "hello"Output: helo Input: String = "Geeks"Output: Geks Naive 4 min read How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from 2 min read How to Remove Special Characters from a String in Ruby? In this article, we will discuss how to remove special characters from a string in Ruby. In Ruby, special characters can be removed from a string using various methods. Special characters typically include punctuation marks, symbols, and non-alphanumeric characters. Let us explore different methods 2 min read How to Remove Double Quotes from String in Scala? This article focuses on discussing how to remove double quotes from a string in Scala. Removing double quotes consists of eliminating all occurrences of double quotes (") from a given string, resulting in a modified string without the double quotes. Remove Double Quotes from String in ScalaBelow are 2 min read Remove Last character from String in Linux In this article, we will discuss how to remove the last character from the string in Linux. In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different comman 3 min read Program to convert Java list of characters to a String in Scala A java list of characters can be converted to a String in Scala by utilizing toString method of Java in Scala. Here, you need to import Scalaâs JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# Scala // Sca 2 min read Like