scala中字符串计数
The range is a set of data from a lower value to a larger value. In Scala, we have an easy method to create a range using to keyword.
范围是从较低值到较大值的一组数据。 在Scala中,我们有一种使用to关键字创建范围的简单方法。
Syntax:
句法:
startchar to endchar
程序创建一系列字符 (Program to create a range of characters)
object myObject {
def main(args: Array[String]) {
val string = ('i' to 'z').toArray
for(i <- 0 to string.length-1)
print(string(i) + " ")
}
}
Output
输出量
i j k l m n o p q r s t u v w x y z
You can also choose the value to be incremented, i.e. you can skip any number of elements while creating this range.
您也可以选择要增加的值,即在创建此范围时可以跳过任何数量的元素。
程序以间隔创建范围 (Program to create a range with interval)
object myObject {
def main(args: Array[String]) {
val string = ('A' to 'K' by 3).toArray
for(i <- 0 to string.length-1)
print(string(i) + " ")
}
}
Output
输出量
A D G J
This range of characters is converted to the array here, we can convert the same to List, vectors, etc using toList and toVector methods respectively.
此范围的字符在此处转换为数组,我们可以分别使用toList和toVector方法将其转换为List,vector等。
创建ASCII范围 (Create ASCII Range)
You can also create a range of ASCII of the value of character within the given range.
您还可以在给定范围内创建字符值的ASCII范围。
Syntax:
句法:
array.range('startChar' , 'endChar')
程序以创建一系列ASCII值 (Program to create a range of ASCII values)
object myObject {
def main(args: Array[String]) {
val ASCIIrange = Array.range('A', 'K')
for(i <- 0 to ASCIIrange.length-1)
print(ASCIIrange(i) + " ")
}
}
Output
输出量
65 66 67 68 69 70 71 72 73 74
翻译自: https://www.includehelp.com/scala/how-to-create-a-range-of-characters-in-scala.aspx
scala中字符串计数