📜  斯卡拉字符串

📅  最后修改于: 2022-05-13 01:54:34.995000             🧑  作者: Mango

斯卡拉字符串

字符串是一个字符序列。在 Scala 中,String 的对象是不可变的,这意味着一个常量,一旦创建就不能更改。

在 Scala 中创建字符串

在 Scala 中创建字符串有两种方法:

  • 在这里,当编译器遇到一个字符串字面量并创建一个字符串对象str。
    句法:
var str = "Hello! GFG"
or
val str = "Hello! GFG"
  • 在这里,在遇到字符串字面量量之前指定了一个 String 类型。
    句法:
var str: String = "Hello! GFG"
or
val str: String = "Hello! GFG"

注意:如果您需要附加到原始字符串,请使用StringBuilder类。
例子:

Scala
// Scala program to illustrate how to 
// create a string
object Main
{
      
    // str1 and str2 are two different strings
    var str1 = "Hello! GFG"
    val str2: String = "GeeksforGeeks"
    def main(args: Array[String]) 
    {
          
        // Display both strings
        println(str1);
        println(str2);
    }
}


Scala
// Scala program to illustrate how to 
// get the length of the given string
object Main 
{
      
    // str1 and str2 are two strings
    var str1 = "Hello! GFG"
    var str2: String = "GeeksforGeeks"
      
    // Main function
    def main(args: Array[String]) 
    {
          
        // Get the length of str1 and str2 strings
        // using length() function
        var LEN1 = str1.length();
        var LEN2 = str2.length();
          
        // Display both strings with their length
        println("String 1:" + str1 + ", Length :" + LEN1);
        println("String 2:" + str2 + ", Length :" + LEN2);
    }
}


Scala
// Scala program to illustrate how to 
// concatenate strings
object Main 
{
      
    // str1 and str2 are two strings
    var str1 = "Welcome! GeeksforGeeks "
    var str2 = " to Portal"
      
    // Main function
    def main(args: Array[String])
    {
          
        // concatenate str1 and str2 strings
        // using concat() function
        var Newstr = str1.concat(str2);
          
        // Display strings 
        println("String 1:" +str1);
        println("String 2:" +str2);
        println("New String :" +Newstr);
          
        // Concatenate strings using '+' operator
        println("This is the tutorial" + 
                    " of Scala language" + 
                    " on GFG portal");
    }
}


Scala
// Scala program to illustrate how to 
// Creating format string
object Main 
{
      
    // two strings
    var A_name = "Ankita "
    var Ar_name = "Scala|Strings"
    var total = 130
      
    // Main function
    def main(args: Array[String]) 
    {
          
        // using format() function
        println("%s, %s, %d".format(A_name, Ar_name, total));
    }
}


输出:

Hello! GFG
GeeksforGeeks

获取字符串的长度

访问器方法是用于查找有关对象的信息的那些方法。因此, length()方法是 Scala 中的访问器方法,用于查找给定字符串的长度。或者换句话说,length() 方法返回字符串对象中存在的字符数。
句法:

var len1 = str1.length();

例子:

斯卡拉

// Scala program to illustrate how to 
// get the length of the given string
object Main 
{
      
    // str1 and str2 are two strings
    var str1 = "Hello! GFG"
    var str2: String = "GeeksforGeeks"
      
    // Main function
    def main(args: Array[String]) 
    {
          
        // Get the length of str1 and str2 strings
        // using length() function
        var LEN1 = str1.length();
        var LEN2 = str2.length();
          
        // Display both strings with their length
        println("String 1:" + str1 + ", Length :" + LEN1);
        println("String 2:" + str2 + ", Length :" + LEN2);
    }
}

输出:

String 1:Hello! GFG, Length :10
String 2:GeeksforGeeks, Length :13

在 Scala 中连接字符串

当通过添加两个字符串创建新字符串时称为字符串连接。 Scala 提供concat()方法来连接两个字符串,该方法返回一个使用两个字符串。您还可以使用“+”运算符连接两个字符串。
句法:

str1.concat(str2);

或者
句法:

"welcome" + "GFG"

例子:

斯卡拉

// Scala program to illustrate how to 
// concatenate strings
object Main 
{
      
    // str1 and str2 are two strings
    var str1 = "Welcome! GeeksforGeeks "
    var str2 = " to Portal"
      
    // Main function
    def main(args: Array[String])
    {
          
        // concatenate str1 and str2 strings
        // using concat() function
        var Newstr = str1.concat(str2);
          
        // Display strings 
        println("String 1:" +str1);
        println("String 2:" +str2);
        println("New String :" +Newstr);
          
        // Concatenate strings using '+' operator
        println("This is the tutorial" + 
                    " of Scala language" + 
                    " on GFG portal");
    }
}

输出:

String 1:Welcome! GeeksforGeeks 
String 2: to Portal
New String :Welcome! GeeksforGeeks  to Portal
This is the tutorial of Scala language on GFG portal

创建格式字符串

当您在字符串中需要格式编号或值时,您将使用printf()format()方法。除了这些方法之外,String 类还提供了一个名为 format() 方法的方法,该方法返回一个 String 对象而不是 PrintStream 对象。
例子:

斯卡拉

// Scala program to illustrate how to 
// Creating format string
object Main 
{
      
    // two strings
    var A_name = "Ankita "
    var Ar_name = "Scala|Strings"
    var total = 130
      
    // Main function
    def main(args: Array[String]) 
    {
          
        // using format() function
        println("%s, %s, %d".format(A_name, Ar_name, total));
    }
}

输出:

Ankita , Scala|Strings, 130

一些重要的字符串函数

FunctionDescription
char charAt(int index)This function returns the character at the given index.
String replace(char ch1, char ch2)This function returns a new string in which the element of ch1 is replaced by the ch2.
String[] split(String reg)This function splits the string around matches of the given regular expression.
String substring(int i)This function returns a new string that is a substring of the given string.
String trim()This function returns a copy of the string, with starting and ending whitespace removed.
boolean startsWith(String prefix)This function is used to check if the given string starts with the specified prefix or not.