本系列的全部内容来源于《Scala编程实战》一书。这个系列仅仅将书中对自己工作有用的代码进行了记录,以方便不时之需时进行查询。
package Strings
object example_1 extends App {
println("Hello World");
println("Hello World".getClass.getSimpleName());
println("Hello World".getClass.getName());
val genome = "Genome";
println(genome.length);
genome.foreach(print);
val genomes = "Genome\t" + "Transcriptome\n"
print(genomes);
println(genome.getBytes());
//字节序列
genomes.getBytes.foreach(println);
val result = genomes.filter( _ != 'e');
println(result);
val scalas = "scala".drop(2).take(2).capitalize;
println(scalas);
val target_1 = "genome"
val target_2 = "Genome"
val target_3 = "genome"
if ( target_1 == target_2){
println("target_1 is equal to target_2")
}
else if( target_1 == target_3){
println("target_1 is equal to target_3")
}
println(target_3.toUpperCase());
println(target_1.toLowerCase);
//另一种字符串比较方法
println(target_3.equalsIgnoreCase(target_2));
//创建多行字符串
val lines =
"""
|I'm
|here
|.
""".stripMargin;
print(lines);
val speech =
"""Four score and
# seven years ago
""".stripMargin('#');
print(speech);
val foo =
""" This is
a multiline
String.
""";
println(foo);
println("Hello World".split(' '));
// val split_ = "2018-09-30\t12306\t22.78\t600101\n".split("\t").foreach(println);
val split_ = "2018-09-30\t12306\t22.78\t600101\n".split("\t");
println(split_.toString());
//字符串中的变量代换
val halo = "halo";
print(s"$halo, everyone $genomes!\n");
val number = 100.08;
print(s"you are ${number * 2} now.\n");
print(s"your scores are ${number == 100}.\n");
println("######################################");
case class lowMan(name:String, IQ:Int);
val ll = lowMan("lowB",250);
//格式化输出
println(s"${ll.name} has a surprised IQ is ${ll.IQ}!");
println(f"your scores are $number%.1f.\n");
printf(f"your scores are $number%.1f.\n");
//原样输出
println(raw"2008-02-30\t29.9\t108\t");
println("%s is sequencing at %s!".format(genome,genomes));
val uppers = s"halo $genomes".map(_.toUpper);
println(uppers);
for (c <- s"halo $genomes") yield c.toUpper;
}