scala中字符串计数_Scala中的字符串

scala中字符串计数

Scala字符串 (Scala strings)

A string is a sequence of characters. In Scala, the String object is immutable. This means that the strings in Scala cannot be modified in the program. Its value given at the time of creation will remain constant throughout the program.

字符串是字符序列 。 在Scala中, String对象是不可变的。 这意味着Scala中的字符串无法在程序中修改。 在创建时给出的值将在整个程序中保持不变。

You can think of the string as a sequence of characters i.e. an array of characters that are "0" indexed. This means the first character has index 0, the second character has index 1, and so on. Every single character from the string can be extracted by using its index number.

您可以将字符串视为字符序列,即索引为“ 0”的字符数组。 这意味着第一个字符的索引为0,第二个字符的索引为1,依此类推。 可以使用字符串的索引号提取字符串中的每个单个字符。

For example, suppose we have an array "arr" which defines some text, we can access any character by using its index number.

例如,假设我们有一个定义了一些文本的数组“ arr” ,我们可以使用其索引号访问任何字符。

    def arr = "Include Help is awesome"
    arr(4)      // Output will be u
    arr(11)     // Output will be p
    arr(34)     // Output will give error array out of bond 
                // and a list of error related to that

All operations on the Scala string are defined in the String class. Which can be imported from "java.lang.String".

Scala字符串上的所有操作都在String类中定义。 可以从“ java.lang.String”导入。

There are two syntaxes that can be used to define a string in Scala:

有两种语法可用于在Scala中定义字符串:

1) The object is created and a string is fed to it,

1)创建对象并向其提供一个字符串,

    var name = "Include Help"
    //or 
    val name = "Include Help"  

2) The object of String type is created using specifier and then the string is fed to it,

2)使用说明符创建String类型的对象,然后将字符串提供给它,

    var name : Sting = "Include Help"
    //pr 
    val name : Sting = "Include Help"

Example:

例:

object MyClass {
        // A program to show implementation of scala string 
      def main(args: Array[String]) {
         var name = "Include Help"
         println("Welcome" + name)
         println("the 5th character of you name is" + name(5));
      }
   }

Output

输出量

WelcomeInclude Help
the 5th character of you name isd

Explanation:

说明:

In the above example, we have created a string named name and Fed to it the value "include help". Then we have printed the string using the println() function. Also, we have printed the fifth character of the string using the index selection method of printing character values of a string that we have discussed previously in this tutorial.

在上面的示例中,我们创建了一个名为name的字符串,并将其指定为“ include help”的值。 然后,我们使用println()函数打印了字符串。 同样,我们已经使用索引选择方法打印了字符串的第五个字符,该方法选择了打印字符串的字符值的索引选择方法,这在本教程之前已经讨论过了。

There are many functions defined over the string object in the string class and We will be going through them in the next tutorial.

在字符串类中的字符串对象上定义了许多函数,我们将在下一个教程中介绍它们。

翻译自: https://www.includehelp.com/scala/strings-in-scala.aspx

scala中字符串计数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值