Q:

Write a Java program to create a new string taking specified number of characters from first and last position of a given string

0

Write a Java program to create a new string taking specified number of characters from first and last position of a given string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_String {

  def test(str1: String, n: Int): String = {
    str1.substring(0, n) + str1.substring(str1.length - n, str1.length);
  }

  def main(args: Array[String]): Unit = {
    var str1 = "Welcome";
    var n1 = 3;
    println("The given strings is: " + str1);
    println("The given numbers is: " + n1);
    println("The new string is: " + test(str1, n1));

    str1 = "Scala Programming";
    n1 = 4;
    println("The given strings is: " + str1);
    println("The given numbers is: " + n1);
    println("The new string is: " + test(str1, n1));
  }
}

Sample Output:

The given strings is: Welcome
The given numbers is: 3
The new string is: Welome
The given strings is: Scala Programming
The given numbers is: 4
The new string is: Scalming

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now