scala编程实战笔记(1-字符串)

序言

优点

  1. 现代编程语言,Martin Odersky开发
  2. 纯的OOP语言
  3. 函数式编程(FP)语言
  4. 在JVM上完美运行无缝使用java类库

REPL中运行scala

scala编程实战笔记(1-字符串)_第1张图片
捕获.PNG

基于vmware使用ubuntu16.04平台开发(当你忘记方法时tab键查看可用方法属性)
书中代码下载地址 https://github.com/alvinj

String

引言

类的几个方法

scala> hello.length
res2: Int = 11

scala> hello + "!"
res3: String = hello world!

scala> res3
res4: String = hello world!

scala> res3.foreach(println)
h
e
l
l
o
 
w
o
r
l
d
!

scala> for (c <- hello) println(c)
h
e
l
l
o
 
w
o
r
l
d

scala> hello.getBytes.foreach(println)
104
101
108
108
111
32
119
111
114
108
100
scala> "scala".drop(2).take(2).capitalize
res13: String = Al
  • foreach : 遍历每个元素
  • for循环
  • 获取ASCII码
  • + 也是一种方法,另外的一种调用方法如下:
scala> "plus".+("+")
res11: String = plus+

scala> hello foreach println
h
e
l
l
o
 
w
o
r
l
d

类的方法来自于StringOps、StringLike和WrappedString,其中StringOps分别是后两者的子类。String隐式转换StringOps。

1.1 测试字符串的相等性

  • ==
  • equalsIgnoreCase :忽略大小写
scala> s1 == s2.toLowerCase
res16: Boolean = true

scala> val s2 = "Hello"
s2: String = Hello

scala> val s1 = "hello"
s1: String = hello

scala> s1 ==s2
res17: Boolean = false

scala> s1 == s2.toLowerCase
res18: Boolean = true

scala> s1.equalsIgnoreCase(s2)
res21: Boolean = true

1.2 创建多行字符串

“”“ ”“” ,换行采用管道符号,或者使用自定义的最后面追加.stripMargin(“自定义”)

1.3 分割字符串

  • split:返回数组Array
scala> "hello world".split(" ")
res26: Array[String] = Array(hello, world)
\# map
scala> "hello world".split(" ") map (_.capitalize)
res27: Array[String] = Array(Hello, World)
\# 接受正则表达式为参数,一个或者多个空格
scala> "hello world".split("//s+")
res28: Array[String] = Array(hello world)
# Char
scala> "hello world".split(' ')
res29: Array[String] = Array(hello, world)

1.4 字符串中的变量代换

cala> val name = "Jack"
name: String = Jack

scala> val age = 33
age: Int = 33

scala> val weight = 65
weight: Int = 65

scala> println("$name is $age years old and weights $weight kg")
$name is $age years old and weights $weight kg

scala> println(s"$name is $age years old and weights $weight kg")
Jack is 33 years old and weights 65 kg
  • 在字符串中使用表达式:s
scala> println(s"next year age: ${age +1}")
next year age: 34

scala> case class Student(name:String, id:Int)
defined class Student
scala> val Lily:Student = Student("lily", 1)
Lily: Student = Student(lily,1)
scala> println(s"${Lily.name}: ${Lily.id}")
lily: 1
\# 遇到错误将类toString再打印输出
scala> println(s"$Lily.name: ${Lily.id}")
Student(lily,1).name: 1
  • f
scala> println(f"${Lily.name}: ${Lily.id}%04.0f")
lily: 0001
  • raw:非转义
scala> raw"//s"
res11: String = //s

map

scala> "hello" map (c => (c.toByte + 1).toChar)
res0: String = ifmmp

scala> "hello" map {
     | c => (c.toByte +1).toChar
     | }
res1: String = ifmmp

map传入的是一个函数

scala> def func(c:Char):Char = (c.toByte + 1).toChar
func: (c: Char)Char

scala> "hello".map(func)
res1: String = ifmmp

写一个脚本

IDE 才有用idea+sbt,创建第一个project,sbt下载很多jar(下载很慢就关闭进度):安装好idea和scala插件后,在~目录下有个.sbt的文件ls -a 查看,进入.sbt目录并repositories,编辑如下并保存:

[repositories]
local
osc: http://maven.aliyun.com/nexus/content/groups/public/
typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots

然后idea窗口ctrl+c,重新下载jar。目录如下

scala编程实战笔记(1-字符串)_第2张图片
212.PNG

1.6模式匹配

  • .r创建一个正则pattern
scala> val numPattern = "[0-9]+"
numPattern: String = [0-9]+

scala> val numPattern = "[0-9]+".r
numPattern: scala.util.matching.Regex = [0-9]+

scala> val address = "123, Dt.St 098"
address: String = 123, Dt.St 098

scala> numPattern.findFirstIn(address)
res0: Option[String] = Some(123)
scala> numPattern.findAllIn(address)
res3: scala.util.matching.Regex.MatchIterator = non-empty iterator
  • Regex类
scala> import scala.util.matching.Regex
import scala.util.matching.Regex

scala> val parttern = new Regex("[0-9]+")
parttern: scala.util.matching.Regex = [0-9]+
  • Option/Some/None
    Option相当于一个容器,有一个和多个值得时候返回Some,没有就返回None。Option[String]返回Some[String]或者None

1.7字符串中的替换模式

  • String的方法
cala> val address ="123dede,1212" 
address: String = 123dede,1212
scala> address.replaceAll("[0-9]+", "---")
res0: String = ---dede,---
  • Regex
scala> val pattern = "[0-9]+".r
pattern: scala.util.matching.Regex = [0-9]+

scala> pattern.replaceAllIn(address, "---")
res2: String = ---dede,---

1.8 匹配多个

scala> val parttern = "([a-z]+)---([0-9]+)".r
parttern: scala.util.matching.Regex = ([a-z]+)---([0-9]+)

scala> val parttern(word, num) = "asd---100"
word: String = asd
num: String = 100

1.9 index 语法糖

实际是调用apply

scala> "hello"(1)
res5: Char = e

scala> "hello".charAt(1)
res6: Char = e

scala> "hello".apply(1)
res7: Char = e

你可能感兴趣的:(scala编程实战笔记(1-字符串))