Scala介绍
Scala介绍
官方网址:http://www.scala-lang.org
官网对scala的介绍:
Scala既是面向对象的语言,也是面向函数的语言。scala可以为你在做大量代码重用和扩展是
提供优雅的层次结构,并可以通过高阶函数来实现这样的目标。(高阶函数是函数式编程里的
特性之一,允许函数作为参数传递,也允许函数作为返回值来返回)
Scala创始人 Martin Odersky马丁·奥德斯基
Martin是EPFL(瑞士领先的技术大学,洛桑联邦理工学院)编程研究组的教授。他在整个职
业生涯中一直不断追求着一个目标:让写程序这样一个基础工作变得高效、简单、且令人愉
悦。
他可能比世界上任何一个人写过更多的Java和Scala代码。他编写了javac,这是目前大部分
Java程序员所使用的编译器。他也编写了Scala编译器scalac,可谓是Scala社区飞速发展的基
石。他著有《Programming in Scala》一书,是最畅销的Scala书籍。他曾经就职于IBM研
究院、耶鲁大学、卡尔斯鲁厄大学以及南澳大利亚大学。在此之前,他在瑞士苏黎世联邦理工
学院追随Pascal创始人Niklaus Wirth学习,并于1989年获得博士学位。
对于scala这门语言,有人把它当做Java的延伸版,有人把它当做JVM上的C++,有人觉得这是面
向对象语言和面向函数语言的简单混合,有人觉得这就是Haskell,而且也还不如Haskell
强。(纯的面向函数式编程语言)
下图Martin Odersky马丁·奥德斯基(Scala的发明者,EPFL教授)在2016年夏天的Scala
Day旧金山大会上发出了这张著名的玩笑照片:
这个图片上的翻译是:“Scala唯一的作用是将人引向Haskell”。马丁·奥德斯基以此作为一个
笑话,说他该把Scala改一下名字,叫做Hascalator,还请人设计了一个Logo。
Scala语言的特点
Scala并不适于编程的初级课程。相反,它是为专业程序员定制的强力语言。
1)它是一门现代编程语言,作者是Martin Odersky(javac之父),受到
Java、Ruby、Smalltalk、ML、Haskell、Erlang等语言的影响。
2)它即是一门面向对象(OOP)语言,每个变量都是一个对象,每个“操作符”都是方
法。scala语言在面向对象的方面,要比java更彻底。
它同时也是一门函数式编程(FP)语言,可以将函数作为参数传递。你可以用OOP、FP,或者
两者结合的方式编写代码。
3)Scala代码通过scalac编译成.class文件,然后在JVM上运行,可以无缝使用已有的丰富的
Java类库。即Scala的代码会编译成字节码,运行在Java虚拟机(JVM)上。
4)接触语言的第一天你就能编出有趣的程序,但是这门语言很深奥,随着学习的深入,你会
发现更新、更好的编写代码的方式。Scala会改变你对编程的看法。针对同一任务,可以有很
多种不同的实现方式,并且可读性以及性能都有不一样的体现。
scala编程的一个示例:
Array(1,2,3,4)
如何遍历数组中的元素,你应该这么做:
for (i <- Array(1,2,3,4)) println(i)
Array(1,2,3,4).foreach{print()}
如果尝试将这些元素转换为新的集合,应该使用for/yield表达式或者map方法:
scala> for (i <- Array(1,2,3)) yield i * 2
res0: Array[Int] = Array(2, 4, 6)
scala> Array(1,2,3).map( * 2)
res1: Array[Int] = Array(2, 4, 6)
比如过滤出一个集合中元素小于4的集合,你可以使用filter方法:
scala> val nums = List(1,2,3,4,5).filter(_ < 4)
res2: nums: List[Int] = List(1, 2, 3)
Scala相较于Java而言,则是相信程序员的优化能力。马丁·奥德斯基说:“很多程序员会告诉
我,他们一般会重构他们的Scala代码两三次,甚至三四次。”这听起来似乎非常的没有效
率,但Scala就是这样的语言,每一次重构,代码的性能或者是可读性都会有极高的提升。
Scala不把程序员当傻子。马丁·奥德斯基对于scalca的定位很清楚:“Scala现在是为聪明人创造
的,以后也是为聪明人服务的。"
Scala提供一整套工具,让程序员自由选择,无论是mutable数据结构,immutable数据结构,并
行(parallel)数据结构。然后在这些选择中,Scala再针对他们进行算法层面的特殊优
化。Scala相信程序员的聪明才智,让程序员自行选择合适的结构,以针对变化万千的任务需
求,这点是Scala做得极好的地方。
scala不是一门纯的函数式编程语言,所以有别纯函数式语言的区别之一是:scala提供变量和
常量,而纯函数式编程语言是没有变量这概念的。
之前就有人提到过,Scala新手和老手写出来的代码完全会呈现两种不同的风格,甚至新人根
本不能读懂有经验的Scala程序员所写的代码,有人于是戏称:“太好了,这样的话我们部门
的实习生就不能乱碰我写的代码啦!”但其实不仅风格不同,执行效率差距也一定是巨大的。
Scala语言的应用
kafka - scala - 分布式消息队列,内部代码经常用来处理并发的问题,用scala可以大大简化其代
码。
spark - scala - 处理多线程场景方便,另外spark主要用作内存计算,经常要用来实现复杂的算
法。利用scala这种函数式编程语言可以大大简化代码。
Scala Windows运行环境配置及使用
实现步骤:
1)双击运行安装
2)添加scala安装目录的bin目录路径到系统环境变量中
3)通过cmd命令窗口,输入scala
交互模式
4)可以通过命令行直接输入scala命令,比如:
编译模式
5)也可以先编写 ***.scala文件,通过执行文件来执行命令,比如:
先在e盘下创建一个hello.scala文件
print(“hello scala”);
然后通过cmd执行:
6)也可以先生成 .scala文件,再编译生成 .class 文件,再执行。
println(“hello scala hello world~”)
def main(args: Array[String]): Unit = {
}
object Person{
}
在e盘下,创建demo01.scala文件,内容如下:
在e盘路径下进入cmd命令窗口
然后执行:scalac demo01.scala
执行完后会发现在e盘下多出了对应的class文件
调用执行:scala Person
使用IDE来开发Scala
解压即可使用
创建 scala project
工程创建完毕后,创建scala object
编写代码
运行得到结果
Scala 基础语法一
Scala 基础语法二
Scala函数—上篇
函数的声明
scala 函数通过 def 关键字定义,def前面可以具有修饰符,可以通过private、protected来
控制其访问权限。
注意:没有public,不写默认就是public的。 此外也可跟上override,final等关键字修饰。
函数的返回值
1)函数体中return关键字往往可以省略掉,一旦省略掉,函数将会返回整个函数体中最后一
行表达式的值,这也要求整个函数体的最后一行必须是正确类型的值的表达式。
2)大部分时候scala都可以通过 =符号 来自动推断出返回值的类型,所以通常返回值类型声
明可以省略。
但是注意:如果因为省略了返回值类型造成歧义,则一定要写上返回值声明。
3)如果函数体只有一行内容,则包裹函数体的大括号可以省略
4)如果返回值类型是UNIT,则另一种写法是可以去掉返回值类型和等号,把方法体写在花括
号内,而这时方法内无论返回什么,返回值都是 UNIT。
格式:[private/protected] def 函数名(参数列表):返回值声明 = {函数体}
示例:
//方法的返回值为空
def f1():Unit={
println(“hello scala”);
}
//等价于f1()方法,注意:如果函数没有=号,无论函数体里的返回值是什么,函数的返回值
都是Unit
def f2(){
println(“hello scala”);
}
//定义方法参数类型,返回值类型,及返回值
def f3(a:Int,b:Int):Int={
a+b;
}
//scala可自行推断返回值类型,所以可省略返回值类型
def f4(a:Int,b:Int)={
a+b;
}
//如果函数体只一行内容,可以省了花括号
def f5(a:Int,b:Int)=a+b
//注意下面这种形式,因为没有=号,所以函数的返回值是Unit,即()
def f6(a:Int,b:Int){
a+b
}
默认参数
代码示意:
object Demo21 {
def f1(a:String,b:String="[",c:String="]")={
b+a+c
}
def main(args: Array[String]): Unit = {
print(f1(“hello”))//将打印:[hello]
}
}
函数的种类
1.成员函数
2.本地函数(内嵌在函数内的函数)
3.函数值(匿名函数)
4.高阶函数
成员函数: 函数被使用在类的内部,作为类的一份子,称为类的成员函数
示例:
object Demo15 {
def main(args: Array[String]): Unit = {
val p=new Student();
p.eat();
p.study();
}
// eat() 和study()属于 类Student的成员函数
class Student{
def eat(){
println(“吃饭”)
}
def study(){
println(“学习”)
}
}
}
本地函数:函数内嵌的函数称为本地函数,这样的函数外界无法访问
示例:
object Demo16 {
def main(args: Array[String]): Unit = {
val p=new Student();
p.eat(“肉”);
}
class Student{
def eat(food:String){
//cook函数内嵌在eat函数里,这样的函数称之为本地函数
def cook(food:String):String={
“做熟了的”+food;
}
println(“吃”+cook(food));
}
}
}
函数值 - 匿名函数:
示例:
def f1(a:Int,b:Int):Int={a+b};
//等价于上式的函数
(a:Int,b:Int)=>{a+b};
//如果函数体只有一行代码,可以省去大括号
(a:Int,b:Int)=>a+b;
//如果函数参数列表只有一个参数,小括号有可以省略
a:Int=>a+1;
//如果函数的参数类型可以被推测,则可以省略类型值
//(a,b)=>a+b
def f1(a:Int,b:Int):Int={a+b};
//可以将f1()函数赋值给f2常量
val f2=f1(,);
//可以将f1()函数赋值给f3变量,f3可以更改此函数
var f3=f1(,);
f3=(a:Int,b:Int)=>a*b;
//也可以这样写
val f4=(c:Int,d:Int)=>{c+d}
//注意,下面的写法是将f1的函数值复制给f5,而不是函数赋值给f5
val f5=f1(2,3)
Scala函数—下篇
高阶函数:函数可以作为方法的参数进行传递和调用
示例1:
object Demo01 {
//定义了compute函数,a,b和f函数 三个参数。其中f函数未做实现。
//我们的目的是对传入的a和b参数利用 f函数做运算,但是具体是什么运算需要由用户自己
来指定。
def compute(a:Int,b:Int,f:(Int,Int)=>Int):Int={
f(a,b)
}
def main(args: Array[String]): Unit = {
val f1=(a:Int,b:Int)=>{a+b}
val f2=(a:Int,b:Int)=>{a*b}
val f3=(a:Int,b:Int)=>{a-b}
val f4=(a:Int,b:Int)=>{a/b}
//由下式可以看出,scala中,函数可以当做参数进行传递和调用
val result=compute(2,3,f1)
//下式等价于上式
//val result=compute(2,3,(a,b)=>{a+b})
}
}
示例2:
object Demo02 {
//定义了一个函数,作用是将用户处理后的字符串结果进行打印输出
def handleString(a:String,f:(String)=>String){
println(“处理完后的字符串为:”+ f(a))
}
def main(args: Array[String]): Unit = {
val a=“hello scala”;
handleString(a,(a)=>{a});
handleString(a,(a)=>{a.substring(6)});
handleString(a,(a)=>{a.concat(" 1706")})
}
}
占位符:占位符指的是scala中的下划线_ ,可以用它当作一个或多个参数来使用。
使用_占位符的前提要求:每个参数在函数仅出现一次。
使用下划线时,如果类型可以自动推断出,则不用声明类型。如果无法自动推断类型,则在下
划线后自己来显示声明类型即可。
示例1:
object Demo03 {
def compute(a:Int,b:Int,f:(Int,Int)=>Int):Int={
f(a,b)
}
def handleString(a:String,f:(String)=>String){
println(“处理完后的字符串为:”+ f(a))
}
def main(args: Array[String]): Unit = {
val message=“hello scala”;
//这样用占位符会报错
//handleString(message,()=>{.substring(6)})
//应改为下面的写法
handleString(message,{.substring(6)})
//如果函数体只有一行代码,则还可以将大括号去掉
handleString(message,.substring(6))
//compute的代码可简化如下
compute(2,3,+)
compute(2,3,*)
compute(2,3,-)
// 此外
// val f1=(a:Int,b:Int)=>{a+b}
// 等价于下式:
// val f1=(:Int)+(:Int)
// compute(2, 3, f1)
//再来看一个例子
val list=List(1,3,5,7,9)
list.foreach { x =>print(x) }
list.foreach { _ =>print() }
list.foreach { print() }
list.foreach { x => x*2 }
list.foreach { _*2 }
}
}
递归
示例1:
object Demo07 {
//从1开始做加法,只加偶数,当加和累计超过50时,结束递归
//示意:2+4+6……
def f1(num:Int,sum:Int):Int={
if(sum>50) return sum;
if(num%20){f1(num+1,sum+num)}
else {f1(num+1,sum)}
}
def main(args: Array[String]): Unit = {
//num是用户传入的初始值=1,sum是最后求得的和,初始值是0
val result=f1(1,0)
println(result)
}
}
示例2,用递归方式实现斐波那契数列
//0 1 1 2 3 5 8 13
if(n0) return 0
if(n1) return 1
else f2(n-1)+f2(n-2)
def f2(n:Int):Int={
}
f2(6)
示例3——2 3 4 9 16 81 ?
// n的取值:f(0) f(1) f(2) f(3) f(4) f(5)
//当n为偶数时,f(n)=f(n-2)*f(n-2)
//当n为奇数是,f(n)=f(n-2)*f(n-2)
//当n为奇数是,f(n)=f(n-2)*f(n-2)
if(n0) return 2
if(n1) return 3
else f3(n-2)f3(n-2)
def f3(n:Int):Int={
}
示例4——函数值:2 3 4 9 8 27 16 ?
//当n为偶数时,f(n)=2f(n-2)
//当n为奇数是,f(n)=3*f(n-2)
if(n0) return 2
if(n1) return 3
if(n%20)2f4(n-2)
else 3f4(n-2)
def f4(n:Int):Int={
}
示例5——求 1~n的数字之和
//1 3 6 10 15
//f(0) f(1) f(2) f(3) f(4)
//f(n)=f(n-1)+n+1
if(n0) return 1
else f5(n-1)+n+1
def f5(n:Int):Int={
}
示例6—给定一个初始值n,并设定sum的初始值为0,当求和sum>12时结束递归
//0 1 3 6 10 15
//f(0,0)——n=0,sum=0
//f(1,1)——n=1,sum=1
//f(2,3)——n=2,sum=3
//f(3,6)——n=3,sum=6
//f(n+1,sum+n)
if(sum>12)return sum
else f6(n+1,sum+n)
def f6(n:Int,sum:Int):Int={
} //> f6: (n: Int, sum: Int)Int
f6(0,0) //> res3: Int = 15
示例7:
//给定一个scope范围,计算0~scope范围的整数之和,
//当和>12或者达到scope边界时,结束递归
def f7(n:Int,sum:Int,scope:Int):Int={
if(sum>12)return sum
if(n-1scope)return sum
else f7(n+1,sum+n,scope)
} //> f7: (n: Int, sum: Int, scope: Int)Int
f7(0,0,4) //> res4: Int = 10
scala中,如果在递归时,保证函数体的最后一行为递归调用,则称这样的递归为尾递归。
scala会针对尾递归做优化处理,所以建议在写递归时写成尾递归形式
StringOps
scala.collection.immutable
StringOps
l final s classStringOps s extends l AnyVal h with StringLike[ [ String] ]
This class serves as a wrapper providing Strings with all the operations
found in indexed sequences. Where needed, instances of String object are
implicitly converted into this class.
The difference between this class and WrappedString is that calling
transformer methods such as filter and map will yield a Stringobject,
whereas a WrappedString will remain a WrappedString
Value Members
def* *: (n: Int: ): String
Return the current string concatenated n times.
1.
def ++: [B](that: GenTraversableOnce: [B]): String[B]
[use case] Returns a new string containing the elements from the left hand
operand followed by the elements from the right hand operand.
2.
def ++:: [B >: Char, , : That](that: collection.Traversable [B])
( ( implicit : bf: CanBuildFrom[ [ String, , , B, : That]): That
As with ++, returns a new collection containing the elements from the left
operand followed by the elements from the right operand.
3.
def ++:: [B](that: TraversableOnce: [B]): String[B]
[use case] As with ++, returns a new collection containing the elements
from the left operand followed by the elements from the right operand.
4.
def +:: (elem: : A): String[A]
[use case] A copy of the string with an element prepended.
5.
def /:: [B](z: , B)(op: (B, Char) ) ⇒ ⇒ B): B): B B
Applies a binary operator to a start value and all elements of this string,
going left to right.
6.
def :+: (elem: : A): String[A]
[use case] A copy of this string with an element appended.
7.
def: :\ : [B](z: B)(op: ( Char, , B) B) ⇒ ⇒ B): B): B B
Applies a binary operator to all elements of this string and a start value,
going right to left.
8.
def< <: (that: String: ): Boolean
Returns true if this is less than that
9.
def <=: (that: String: ): Boolean
Returns true if this is less than or equal to that.
10.
def> >: (that: String: ): Boolean
Returns true if this is greater than that.
def >=: (that: String: ): Boolean
Returns true if this is greater than or equal to that.
12.
def addString: (b: StringBuilder: ): StringBuilder
Appends all elements of this string to a string builder.
13.
def addString: (b: StringBuilder, , : sep: String: ): StringBuilder
Appends all elements of this string to a string builder using a separator
string.
14.
def addString: (b: StringBuilder, , : start: String, , : sep: String, , : end: String: ): S S
tringBuilder
Appends all elements of this string to a string builder using start, end,
and separator strings.
15.
def aggregate: [B](z: , B)(seqop: (B, Char) ) ⇒ ⇒ B, B, , combop: (B, B) B) ⇒ ⇒ B): B): B B
Aggregates the results of applying an operator to subsequent elements.
16.
def apply: (index: Int: ): Char
Return element at index n
17.
def asParIterable: : ParIterable[ [ Char] ] 18.
def asParSeq: : ParSeq[ [ Char] ] 19.
def canEqual: (that: Any: ): Boolean
Method called from equality methods, so that user-defined subclasses can
refuse to be equal to other collections of the same kind.
20.
def capitalize: : String
Returns this string with first character converted to upper case
21.
def charAt: (arg0: Int: ): Char 22.
def codePointAt: (arg0: Int: ): Int 23.
def codePointBefore: (arg0: Int: ): Int 24.
def codePointCount: (arg0: Int, , : arg1: Int: ): Int 25.
def collect: [B](pf: PartialFunction, [A, : B]): String[B]
[use case] Builds a new collection by applying a partial function to all
elements of this string on which the function is defined.
26.
def collectFirst: [B](pf: PartialFunction[ [ Char, , : B]): Option [B]
Finds the first element of the string for which the given partial function
is defined, and applies the partial function to it.
27.
def combinations: (n: Int: ): Iterator[ [ String] ]
Iterates over combinations.
28.
def compare: (other: String: ): Int
Result of comparing this with operand that.
29.
def compareTo: (that: String: ): Int
Result of comparing this with operand that.
30.
def compareToIgnoreCase: (arg0: : String): Int 31.
def concat: (arg0: : String): String 32.
def contains: (elem: Any: ): Boolean
Tests whether this string contains a given value as an element.
33.
def containsSlice: [B](that: GenSeq: [B]): Boolean
Tests whether this string contains a given sequence as a slice.
def contentEquals: (arg0: : CharSequence): Boolean 35.
def contentEquals: (arg0: : StringBuffer): Boolean 36.
def copyToArray: (xs: Array, [A], : start: Int, , : len: Int: ): Unit
[use case] Copies elements of this string to an array.
37.
def copyToArray: (xs: Array: [A]): Unit
[use case] Copies values of this string to an array.
38.
def copyToArray: (xs: Array, [A], : start: Int: ): Unit
[use case] Copies values of this string to an array.
39.
def copyToBuffer: [B >: Char: ](dest: Buffer: [B]): Unit
Copies all elements of this string to a buffer.
40.
def corresponds: [B](that: GenSeq [B])(p: ( Char, , B) B) ⇒ ⇒ Boolean Boolean: ): Boolean
Tests whether every element of this string relates to the corresponding
element of another sequence by satisfying a test predicate.
41.
def count (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Int
Counts the number of elements in the string which satisfy a predicate.
42.
def diff: (that: collection.Seq[ [ Char: ]): String[ Char] ]
[use case] Computes the multiset difference between this string and another
sequence.
43.
def distinct: : String
Builds a new string from this string without any duplicate elements.
44.
def drop: (n: Int: ): String
Selects all elements except first n ones.
45.
def dropRight: (n: Int: ): String
Selects all elements except last n ones.
46.
def dropWhile (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): String
Drops longest prefix of elements that satisfy a predicate.
47.
def endsWith: [B](that: GenSeq: [B]): Boolean
Tests whether this string ends with the given sequence.
48.
def equals: (arg0: Any: ): Boolean
The equality method for reference types.
49.
def equalsIgnoreCase: (arg0: : String): Boolean 50.
def exists (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Boolean
Tests whether a predicate holds for some of the elements of this string.
51.
def filter (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): String
Selects all elements of this string which satisfy a predicate.
52.
def filterNot (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): String
Selects all elements of this string which do not satisfy a predicate.
53.
def find (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Option[ [ Char] ]
Finds the first element of the string satisfying a predicate, if any.
54.
def flatMap[B](f: (A) [B](f: (A) ⇒ ⇒ GenTraversableOnce GenTraversableOnce: [B]): String[B]
[use case] Builds a new collection by applying a function to all elements
of this string and using the elements of the resulting collections.
55.
def fold: [A1 >: Char: ](z: , A1)(op: (A1, A1) A1) ⇒ ⇒ A1): A1): A1
Folds the elements of this string using the specified associative binary
operator.
def foldLeft: [B](z: , B)(op: (B, Char) ) ⇒ ⇒ B): B): B B
Applies a binary operator to a start value and all elements of this string,
going left to right.
57.
def foldRight: [B](z: B)(op: ( Char, , B) B) ⇒ ⇒ B): B): B B
Applies a binary operator to all elements of this string and a start value,
going right to left.
58.
def forall (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Boolean
Tests whether a predicate holds for all elements of this string.
59.
def foreach(f: (A) (f: (A) ⇒ ⇒ Unit Unit: ): Unit
[use case] Applies a function f to all elements of this string.
60.
def format: (args: Any: *): String
Uses the underlying string as a pattern (in a fashion similar to printf in
C), and uses the supplied arguments to fill in the holes.
61.
def formatLocal: (l: , Locale, : args: Any: ): String
Like format(args) but takes an initial Locale parameter which influences
formatting as in java.lang.String’s format.
62.
def getBytes: (): Array[ [ Byte] ] 63.
def getBytes: (arg0: : Charset): Array[ [ Byte] ] 64.
def getBytes: (arg0: : String): Array[ [ Byte] ] 65.
def getChars: (arg0: Int, , : arg1: Int, , : arg2: Array[ [ Char, ], : arg3: Int: ): Unit 66.
def getClass: (): Class: [_ <: AnyVal] ]
Returns the runtime class representation of the object.
67.
def groupBy K ⇒ ⇒ K): K): Map, [K, String] ]
Partitions this string into a map of strings according to some
discriminator function.
68.
def grouped: (size: Int: ): Iterator[ [ String] ]
Partitions elements in fixed size strings.
69.
def hasDefiniteSize: : Boolean
Tests whether this string is known to have a finite size.
70.
def hashCode: (): Int
The hashCode method for reference types.
71.
def head: : Char
Selects the first element of this string.
72.
def headOption: : Option[ [ Char] ]
Optionally selects the first element.
73.
def ifParSeq [R](isbody: ( ParSeq[ [ Char]) ]) ⇒ ⇒ R): R):
( ( TraversableOps[ [ Char ])#Otherwise[R]
74.
def indexOf: (elem: Char, , : from: Int: ): Int
[use case] Finds index of first occurrence of some value in this string
after or at some start index.
75.
def indexOf: (elem: Char: ): Int
[use case] Finds index of first occurrence of some value in this string.
76.
def indexOfSlice: [B >: Char: ](that: GenSeq, [B], : from: Int: ): Int
Finds first index after or at a start index where this string c
Finds first index after or at a start index where this string contains a
given sequence as a slice.
77.
def indexOfSlice: [B >: Char: ](that: GenSeq: [B]): Int
Finds first index where this string contains a given sequence as a slice.
78.
def indexWhere (p: ( Char) ) ⇒ ⇒ Boolean Boolean, , : from: Int: ): Int
Finds index of the first element satisfying some predicate after or at some
start index.
79.
def indexWhere (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Int
Finds index of first element satisfying some predicate.
80.
def indices: : Range
Produces the range of all indices of this sequence.
81.
def init: : String
Selects all elements except the last.
82.
def inits: : Iterator[ [ String] ]
Iterates over the inits of this string.
83.
def intern: (): String 84.
def intersect: (that: collection.Seq[ [ Char: ]): String[ Char] ]
[use case] Computes the multiset intersection between this string and
another sequence.
85.
def isDefinedAt: (idx: Int: ): Boolean
Tests whether this string contains given index.
86.
def isEmpty: : Boolean
Tests whether this string is empty.
87.
def isParIterable: : Boolean 88.
def isParSeq: : Boolean 89.
def isParallel: : Boolean 90.
l final def isTraversableAgain: : Boolean
Tests whether this string can be repeatedly traversed.
91.
def iterator: : Iterator[ [ Char] ]
Creates a new iterator over all elements contained in this iterable object.
92.
def last: : Char
Selects the last element.
93.
def lastIndexOf: (elem: Char, , : end: Int: ): Int
[use case] Finds index of last occurrence of some value in this string
before or at a given end index.
94.
def lastIndexOf: (elem: Char: ): Int
[use case] Finds index of last occurrence of some value in this string.
95.
def lastIndexOfSlice: [B >: Char: ](that: GenSeq, [B], : end: Int: ): Int
Finds last index before or at a given end index where this string contains
a given sequence as a slice.
96.
def lastIndexOfSlice: [B >: Char: ](that: GenSeq: [B]): Int
Finds last index where this string contains a given sequence as a slice.
97.
def lastIndexWhere (p: ( Char) ) ⇒ ⇒ Boolean Boolean, , : end: Int: ): Int
Finds index of last element satisfying some predicate before or at given
end index.
def lastIndexWhere (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Int
Finds index of last element satisfying some predicate.
99.
def lastOption: : Option[ [ Char] ]
Optionally selects the last element.
100.
def length: : Int
The length of the string.
101.
def lengthCompare: (len: Int: ): Int
Compares the length of this string to a test value.
102.
def lines: : Iterator[ [ String] ]
Return all lines in this string in an iterator, excluding trailing line end
characters, i.
103.
def linesIterator: : Iterator[ [ String] ]
Return all lines in this string in an iterator, excluding trailing line end
characters, i.
104.
def linesWithSeparators: : Iterator[ [ String] ]
Return all lines in this string in an iterator, including trailing line end
characters.
105.
def map[B](f: (A) [B](f: (A) ⇒ ⇒ B): B): String[B]
[use case] Builds a new collection by applying a function to all elements
of this string.
106.
def matches: (arg0: : String): Boolean 107.
def max: : A A
[use case] Finds the largest element.
108.
def maxBy B ⇒ ⇒ B)( B)( implicit : cmp: Ordering: [B]): Char 109.
def min: : A A
[use case] Finds the smallest element.
110.
def minBy B ⇒ ⇒ B)( B)( implicit : cmp: Ordering: [B]): Char 111.
def mkString: : String
Displays all elements of this string in a string.
112.
def mkString: (sep: String: ): String
Displays all elements of this string in a string using a separator string.
113.
def mkString: (start: String, , : sep: String, , : end: String: ): String
Displays all elements of this string in a string using start, end, and
separator strings.
114.
def nonEmpty: : Boolean
Tests whether the string is not empty.
115.
def offsetByCodePoints: (arg0: Int, , : arg1: Int: ): Int 116.
def padTo: (len: Int, , : elem: : A): String[A]
[use case] A copy of this string with an element value appended until a
given target length is reached.
117.
def par: : ParSeq[ [ Char] ]
Returns a parallel implementation of this collection.
118.
def partition (p: ( Char) ) ⇒ ⇒ Boolean Boolean ): ( String, , String) )
Partitions this string in two strings according to a predicate.
119.
def patch: (from: Int, , : that: GenSeq, [A], : replaced: Int: ): String[A]
[use case] Produces a new string where a slice of elements in this string
is replaced by another sequence.
120.
def permutations: : Iterator[ [ String] ]
Iterates over distinct permutations.
121.
def prefixLength (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Int
Returns the length of the longest prefix whose elements all satisfy some
predicate.
122.
def product: : A A
[use case] Multiplies up the elements of this collection.
123.
defr r: (groupNames: String: *): Regex
You can follow a string with .r(g1, … , gn), turning it into a Regex,
with group names g1 through gn.
124.
defr r: : Regex
You can follow a string with .r, turning it into a Regex.
125.
def reduce: [A1 >: Char, ](op: (A1, A1) A1) ⇒ ⇒ A1): A1): A1
Reduces the elements of this string using the specified associative binary
operator.
126.
def reduceLeft: B >: Char, ⇒ ⇒ B): B): B B
Applies a binary operator to all elements of this string, going left to
right.
127.
def reduceLeftOption: B >: Char, ⇒ ⇒ B): B): Option [B]
Optionally applies a binary operator to all elements of this string, going
left to right.
128.
def reduceOption: [A1 >: Char, ](op: (A1, A1) A1) ⇒ ⇒ A1): A1): Option [A1]
Reduces the elements of this string, if any, using the specified
associative binary operator.
129.
def reduceRight: [B >: Char ](op: ( Char, , B) B) ⇒ ⇒ B): B): B B
Applies a binary operator to all elements of this string, going right to
left.
130.
def reduceRightOption: [B >: Char ](op: ( Char, , B) B) ⇒ ⇒ B): B): Option [B]
Optionally applies a binary operator to all elements of this string, going
right to left.
131.
def regionMatches: (arg0: Boolean, , : arg1: Int, , : arg2: , String, : arg3: Int, , arg4:
Int: ): Boolean
132.
def regionMatches: (arg0: Int, , : arg1: , String, : arg2: Int, , : arg3: Int: ): Boolean 133.
def replace: (arg0: , CharSequence, : arg1: : CharSequence): String 134.
def replace: (arg0: Char, , : arg1: Char: ): String 135.
def replaceAll: (arg0: , String, : arg1: : String): String 136.
def replaceAllLiterally: (literal: String, , : replacement: String: ): String
Replace all literal occurrences of literal with the string replacement.
137.
def replaceFirst: (arg0: , String, : arg1: : String): String 138.
val repr: : String
the actual representation of this string operations object.
139.
def reverse: : String
Returns new string wih elements in reversed order.
140.
def reverseIterator: : Iterator[ [ Char] ]
An iterator yielding elements in reversed order.
141.
def reverseMap[B](f: (A) [B](f: (A) ⇒ ⇒ B): B): String[B]
[use case] Builds a new collection by applying a function to all elements
of this string and collecting the results in reversed order.
142.
def sameElements: (that: GenIterable: [A]): Boolean
[use case] Checks if the other iterable collection contains the same
elements in the same order as this string.
143.
def scan: [B >: Char, , : That](z: , B)(op: (B, B) B) ⇒ ⇒ B) B)
( ( implicit : cbf: CanBuildFrom[ [ String, , , B, : That]): That
Computes a prefix scan of the elements of the collection.
144.
def scanLeft, [B, : That](z: , B)(op: (B, Char) ) ⇒ ⇒ B) B)
( ( implicit : bf: CanBuildFrom[ [ String, , , B, : That]): That
Produces a collection containing cumulative results of applying the
operator going left to right.
145.
def scanRight, [B, : That](z: B)(op: ( Char, , B) B) ⇒ ⇒ B) B)
( ( implicit : bf: CanBuildFrom[ [ String, , , B, : That]): That
Produces a collection containing cumulative results of applying the
operator going right to left.
146.
def segmentLength (p: ( Char) ) ⇒ ⇒ Boolean Boolean, , : from: Int: ): Int
Computes length of longest segment whose elements all satisfy some
predicate.
147.
def seq: : WrappedString
A version of this collection with all of the operations implemented
sequentially (i.
148.
def size: : Int
The size of this string, equivalent to length.
149.
def slice: (from: Int, , : until: Int: ): String
Selects an interval of elements.
150.
def sliding: (size: Int, , : step: Int: ): Iterator[ [ String] ]
Groups elements in fixed size blocks by passing a “sliding window” over
them (as opposed to partitioning them, as is done in grouped.
151.
def sliding: (size: Int: ): Iterator[ [ String] ]
Groups elements in fixed size blocks by passing a “sliding window” over
them (as opposed to partitioning them, as is done in grouped.
152.
def sortBy B ⇒ ⇒ B)( B)( implicit : ord: math.Ordering: [B]): String
Sorts this String according to the Ordering which results from transforming
an implicitly given Ordering with a transformation function.
153.
def sortWith (lt: ( Char, , Char) ) ⇒ ⇒ Boolean Boolean: ): String
Sorts this string according to a comparison function.
154.
def sorted: [B >: Char ]( implicit : ord: math.Ordering: [B]): String
Sorts this string according to an Ordering.
155.
def span (p: ( Char) ) ⇒ ⇒ Boolean Boolean ): ( String, , String) )
Splits this string into a prefix/suffix pair according to a predicate.
156.
def split: (separators: Array[ [ Char: ]): Array[ [ String] ] 157.
def split: (separator: Char: ): Array[ [ String] ] 158.
def splitAt: (n: Int ): ( String, , String) )
Splits this string into two at a given position.
159.
def startsWith: [B](that: GenSeq, [B], : offset: Int: ): Boolean
Tests whether this string contains the given sequence at a given index.
160.
def startsWith: [B](that: GenSeq: [B]): Boolean
Tests whether this string starts with the given sequence.
161.
def stringPrefix: : String
Defines the prefix of this object’s toString representation.
162.
def stripLineEnd: : String
Strip trailing line end character from this string if it has one.
163.
def stripMargin: : String
For every line in this string:
164.
def stripMargin: (marginChar: Char: ): String
For every line in this string:
165.
def stripPrefix: (prefix: String: ): String
Returns this string with the given prefix stripped.
166.
def stripSuffix: (suffix: String: ): String
Returns this string with the given suffix stripped.
167.
def subSequence: (arg0: Int, , : arg1: Int: ): CharSequence 168.
def substring: (arg0: Int, , : arg1: Int: ): String 169.
def substring: (arg0: Int: ): String 170.
def sum: : A A
[use case] Sums up the elements of this collection.
171.
def tail: : String
Selects all elements except the first.
172.
def tails: : Iterator[ [ String] ]
Iterates over the tails of this string.
173.
def take: (n: Int: ): String
Selects first n elements.
174.
def takeRight: (n: Int: ): String
Selects last n elements.
175.
def takeWhile (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): String
Takes longest prefix of elements that satisfy a predicate.
176.
def to: [Col[_]]: Col[A]
[use case] Converts this string into another by copying all elements.
177.
def toArray: : Array [A]
[use case] Converts this string to an array.
178.
def toBoolean: : Boolean 179.
def toBuffer: [A1 >: Char: ]: Buffer [A1]
Converts this string to a mutable buffer.
def toByte: : Byte 181.
def toCharArray: (): Array[ [ Char] ] 182.
def toDouble: : Double 183.
def toFloat: : Float 184.
def toIndexedSeq: : IndexedSeq[ [ Char] ]
Converts this string to an indexed sequence.
185.
def toInt: : Int 186.
def toIterable: : collection.Iterable[ [ Char] ]
Converts this string to an iterable collection.
187.
def toIterator: : Iterator[ [ Char] ]
Returns an Iterator over the elements in this string.
188.
def toList: : scala.List[ [ Char] ]
Converts this string to a list.
189.
def toLong: : Long 190.
def toLowerCase: (): String 191.
def toLowerCase: (arg0: : Locale): String 192.
def toMap, [T, : U]: collection.Map, [T, U]
[use case] Converts this string to a map.
193.
def toParArray: : ParArray[ [ Char] ] 194.
def toSeq: : collection.Seq[ [ Char] ]
Converts this string to a sequence.
195.
def toSet: [B >: Char: ]: Set [B]
Converts this string to a set.
196.
def toShort: : Short 197.
def toStream: : Stream[ [ Char] ]
Converts this string to a stream.
198.
def toString: (): String
Converts this string to a string.
199.
def toTraversable: : collection.Traversable[ [ Char] ]
Converts this string to an unspecified Traversable.
200.
def toUpperCase: (): String 201.
def toUpperCase: (arg0: : Locale): String 202.
def toVector: : scala.Vector[ [ Char] ]
Converts this string to a Vector.
203.
def trim: (): String 204.
def union: (that: collection.Seq[ [ Char: ]): String[ Char] ]
[use case] Produces a new sequence which contains all elements of this
string and also all elements of a given sequence.
205.
def updated: (index: Int, , : elem: : A): String[A]
[use case] A copy of this string with one single replaced element.
206.
def view: (from: Int, , : until: Int: ): SeqView[ [ Char, , String] ]
Creates a non-strict view of a slice of this string.
def view: : SeqView[ [ Char, , String] ]
Creates a non-strict view of this string.
208.
def withFilter (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): FilterMonadic[ [ Char, , String] ]
Creates a non-strict filter of this string.
209.
def zip: [B](that: GenIterable: [B]): , String[(A, B)]
[use case] Returns a string formed from this string and another iterable
collection by combining corresponding elements in pairs.
210.
def zipAll [B]
: (that: collection.Iterable, [B], : thisElem: , A, : thatElem: : B): , String[(A, B)]
[use case] Returns a string formed from this string and another iterable
collection by combining corresponding elements in pairs.
211.
def zipWithIndex: : , String[(A, Int )]
[use case] Zips this string with its indices.
212.
Shadowed Implicit Value Members
l final def+ +: (arg0: Any: ): String 1.
def compareTo: (arg0: : String): Int 2.
def contains: (arg0: : CharSequence): Boolean 3.
def endsWith: (arg0: : String): Boolean 4.
def filter (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): TraversableOnce[ [ Char] ] 5.
def flatMap B ⇒ ⇒ GenTraversableOnce GenTraversableOnce: [B]): TraversableOnce [B] 6.
def indexOf: (arg0: , String, : arg1: Int: ): Int 7.
def indexOf: (arg0: : String): Int 8.
def indexOf: (arg0: Int, , : arg1: Int: ): Int 9.
def indexOf: (arg0: Int: ): Int 10.
def isEmpty: (): Boolean 11.
def lastIndexOf: (arg0: , String, : arg1: Int: ): Int 12.
def lastIndexOf: (arg0: : String): Int 13.
def lastIndexOf: (arg0: Int, , : arg1: Int: ): Int 14.
def lastIndexOf: (arg0: Int: ): Int 15.
def length: (): Int 16.
def map B ⇒ ⇒ B): B): TraversableOnce [B] 17.
def split: (arg0: : String): Array [String] 18.
def split: (arg0: , String, : arg1: Int: ): Array [String] 19.
def startsWith: (arg0: : String): Boolean 20.
def startsWith: (arg0: , String, : arg1: Int: ): Boolean 21.
def toString: (): String
Creates a String representation of this object.
22.
def withFilter (p: ( Char) ) ⇒ ⇒ Boolean Boolean: ): Iterator[ [ Char] ] 23.
Deprecated Value Members
def /:\ : [A1 >: Char: ](z: , A1)(op: (A1, A1) A1) ⇒ ⇒ A1): A1): A1
def getBytes: (arg0: Int, , : arg1: Int, , : arg2: Array[ [ Byte, ], : arg3: Int: ): Unit 2.
Scala programming documentation. Copyright © 2003-2013 EPFL, with
contributions from Typesafe.
来自 http://www.scala-lang.org/api/2.10.4/scala/collection/immutable/StringOps.html
RichInt
scala.runtime
RichInt
Instance Constructors
: newRichInt(self: Int) ) 1.
Type Members
typep ResultWithoutStep = = collection.immutable.Range 1.
Value Members
def< <: (that: Int: ): Boolean
Returns true if this is less than that
1.
def <=: (that: Int: ): Boolean
Returns true if this is less than or equal to that.
2.
def> >: (that: Int: ): Boolean
Returns true if this is greater than that.
3.
def >=: (that: Int: ): Boolean
Returns true if this is greater than or equal to that.
4.
def abs: : Int
Computes the absolute value of this.
5.
def byteValue: (): Byte 6.
def compare: (y: Int: ): Int
Result of comparing this with operand that.
7.
def compareTo: (that: Int: ): Int
Result of comparing this with operand that.
8.
def doubleValue: (): Double 9.
def floatValue: (): Float 10.
def getClass: (): Class: [_ <: AnyVal] ]
Returns the runtime class representation of the object.
11.
def intValue: (): Int 12.
def isValidByte: : Boolean
Returns true iff this has a zero fractional part, and is within the range
of scala.Byte MinValue and MaxValue; otherwise returns false.
13.
def isValidChar: : Boolean
Returns true iff this has a zero fractional part, and is within the range
of scala.Char MinValue and MaxValue; otherwise returns false.
14.
def isValidInt: : Boolean
Returns true iff this has a zero fractional part, and is within the range
of scala.Int MinValue and MaxValue; otherwise returns false.
15.
def isValidShort: : Boolean
Returns true iff this has a zero fractional part, and is within the range
of scala.Short MinValue and MaxValue; otherwise returns false.
16.
def isWhole: (): Boolean 17.
def longValue: (): Long
def max: (that: Int: ): Int 19.
def min: (that: Int: ): Int 20.
val self: : Int 21.
def shortValue: (): Short 22.
def signum: : Int 23.
def to: (end: Int, , : step: Int: ): Inclusive 24.
def to: (end: Int: ): Inclusive 25.
def toBinaryString: : String 26.
def toByte: : Byte
Returns the value of this as a scala.Byte.
27.
def toChar: : Char
Returns the value of this as a scala.Char.
28.
def toDouble: : Double
Returns the value of this as a scala.Double.
29.
def toFloat: : Float
Returns the value of this as a scala.Float.
30.
def toHexString: : String 31.
def toInt: : Int
Returns the value of this as an scala.Int.
32.
def toLong: : Long
Returns the value of this as a scala.Long.
33.
def toOctalString: : String 34.
def toShort: : Short
Returns the value of this as a scala.Short.
35.
def toString: (): String
Returns a string representation of the object.
36.
def underlying: (): AnyRef 37.
def until: (end: Int, , : step: Int: ): collection.immutable.Range 38.
def until: (end: Int: ): collection.immutable.Range 39.
Scala programming documentation. Copyright © 2003-2013 EPFL, with contributions from Typesafe.
来自 http://www.scala-lang.org/api/2.10.4/scala/runtime/RichInt.html