Write a Scala program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back

  • برمجة سكالا

Write a Scala program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.

الأجوبة

object scala_basic {
  def test(str1: String): String =
    {
    if (str1.length < 3) str1 * 3
    else str1.substring(0, 3) + str1 + str1.substring(0, 3)
    }
   def main(args: Array[String]): Unit = {
      println("Result: " + test("Scala"));
      println("Result: " + test("abcd"));
      println("Result: " + test("ab"));
      println("Result: " + test("a"));
    }
}

Sample Output:

Result: ScaScalaSca
Result: abcabcdabc
Result: ababab
Result: aaa
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...