Scala之字符串【一】

        本系列的全部内容来源于《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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值