zoukankan      html  css  js  c++  java
  • ruby 学习 -- string --1

    # define
    french_string = "il xc3xa9tait une fois"
    long_string = <<EOF
    Here is a long string
    With many paragraphs
    EOF
    
    puts long_string.empty?
    puts long_string.include? "many"
    
    puts french_string + long_string
    
    # concatenate
    hash = { key1: "val1", key2: "val2" }
    string = ""
    str2 = ""
    hash.each{|k,v| string << k.to_s << " is " << v << "
    " }
    hash.each{|k,v| str2 << "#{k}" << " is " << "#{v}" << "
    "}
    puts string
    puts str2
    
    # join
    data = ['1', '2', '3']
    s = ''
    data.each { |x| s << x << ' and a '}
    puts s # => "1 and a 2 and a 3 and a "
    puts data.join(' and a ')
    
    # number
    number = 5
    puts "The number is #{number}." # => "The number is 5."
    puts "The number is #{5}." # => "The number is 5."
    puts "The number after #{number} is #{number.next}."
    # => "The number after 5 is 6."
    puts "The number prior to #{number} is #{number-1}."
    # => "The number prior to 5 is 4."
    puts "We're ##{number}!" # => "We're #5!"
    puts "I've set x to #{x = 5; x += 1}."
    
    # Escaping
    puts "#{foo}"
    puts '#{foo}'
    # puts "#{foo}" # error because no variable of foo defined.
    template = 'Oceania has always been at war with %s.'
    puts template % 'Eurasia' # => "Oceania has always been at war with Eurasia."
    
    puts 'To 2 decimal places: %.4f' % Math::PI
    puts 'Zero-padded: %.3d' % Math::PI

    JSP, ASP type

    require 'erb'
    template = ERB.new %q{Chunky <%= food %>!}
    food = "bacon"
    puts template.result(binding) # => "Chunky bacon!"
    food = "peanut butter"
    puts template.result(binding) # => "Chunky peanut butter!"
    puts template.result

     reverse, reverse!, split

     reverse 和 reverse! 的区别:reverse 不改变 string 本身, reverse! 相当于 s=s.reverse

    s = ".sdrawkcab si gnirts sihT"
    puts s.reverse
    puts s
    puts s.reverse!
    puts s.split(/(s+)/) # ["This", " ", "string", " ", "is", " ", "backwards."]
    puts s.split(/s+/) # => ["This", "sting", "is", "backwards."]
    puts s.split(' ')     # => ["This", "sting", "is", "backwards."]

    八进制 和 十六进制 的定义

    octal = "00011020"
    octal.each_byte { |x| puts x }
    # 0
    # 1
    # 8
    # 16
    hexadecimal = "x00x01x10x20"
    hexadecimal.each_byte { |x| puts x }
    # 0
    # 1
    # 16
    # 32

    This makes it possible to represent UTF-8 characters even when you can’t type them or display them in your terminal.

    Try running this program, and then opening the generated file smiley.html in your web browser:

    open('smiley.html', 'wb') do |f|
        f << '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">'
        f << "xe2x98xBA"
    end

    特殊字符:

    "a" == "x07" # => true # ASCII 0x07 = BEL (Sound system bell)
    "" == "x08" # => true # ASCII 0x08 = BS (Backspace)
    "e" == "x1b" # => true # ASCII 0x1B = ESC (Escape)
    "f" == "x0c" # => true # ASCII 0x0C = FF (Form feed)
    "
    " == "x0a" # => true # ASCII 0x0A = LF (Newline/line feed)
    "
    " == "x0d" # => true # ASCII 0x0D = CR (Carriage return)
    "	" == "x09" # => true # ASCII 0x09 = HT (Tab/horizontal tab)
    "v" == "x0b" # => true # ASCII 0x0B = VT (Vertical tab)

    ruby里,如果是可读的ASCII字符,即使是以十六进制表示,print出来的还是可读字符。不可读,输出的为十六进制(以x开头)字符表示形式。

    "x10x11xfexff" # => "u0010u0011xFExFF"
    "x48145x6cx6c157x0a" # => "Hello
    "

    为了避免混淆,统一的,单反斜线由双反斜线表示:  => \

    "\".size # => 1
    "\" == "x5c" # => true
    "\n"[0] == ?\ # => true
    "\n"[1] == ?n # => true
    "\n" =~ /
    / # => nil

    组合键的表示. =~ 是正则匹配符号。

    "C-aC-bC-c" # => "u0001u0002u0003"
    "M-aM-bM-c" # => "xE1xE2xE3"
    
    ?C-a # => "u0001"
    ?M-z # => "xFA"
    
    contains_control_chars = /[C-a-C-^]/
    'Foobar' =~ contains_control_chars # => nil
    "FooC-zbar" =~ contains_control_chars # => 3
    def snoop_on_keylog(input)
        input.each_char do |b|
            case b
              when ?C-c; puts 'Control-C: stopped a process?'
              when ?C-z; puts 'Control-Z: suspended a process?'
              when ?
    ; puts 'Newline.'
              when ?M-x; puts 'Meta-x: using Emacs?'
            end
         end
    end
    
    snoop_on_keylog("ls -ltR03emacsHello12370rot13-other-window1232")
    # Control-C: stopped a process?
    # Newline.
    # Meta-x: using Emacs?
    # Newline.
    # Control-Z: suspended a process?

    字符串定义

    puts "foo	bar"
    # foo bar
    puts %{foo	bar}
    # foo bar
    puts %Q{foo	bar}
    # foo bar
    puts 'foo	bar'
    # foo	bar
    puts %q{foo	bar}
    # foo	bar

    统计单词个数。

    class String
        def word_count
            frequencies = Hash.new(0)
            self.downcase.scan(/(w+([-'.]w+)*)/) { |word, ignore| frequencies[word] += 1 }
            return frequencies
        end
    end
    puts %{"The F.B.I. fella--he's quite the man-about-town."}.word_count
    # => {"f.b.i"=>1, "fella"=>1, "he's"=>1,
    # "quite"=>1, "the"=>2, "man-about-town"=>1}

    end with 2.9 cookbook

    to be continued...

  • 相关阅读:
    C++ tinyXml直接解析XML字符串
    留言板小程序开发笔记3
    如何去除 gvim 的 acp和 "option omnifunc is not set" 的问题
    如何修改bootstrap模态框的backdrop蒙版区域的颜色?
    Windows下卸载软件时提示 等待先前的卸载完成? 终止 dllhost.exe 进程
    thinkphp中的__DIR__ __ROOT__ __APP__ __MODULE__ APP_PATH LIB_PATH MODULE_PATH 等是在哪里定义的?
    thinkphp中的Ueditor的使用, 以及如何传递编辑器内容到后台?
    留言板小程序开发笔记2
    分页器的js实现代码 bootstrap Paginator.js
    windows的gvim总是报错: +iconv fencview.vim
  • 原文地址:https://www.cnblogs.com/snow-backup/p/4998468.html
Copyright © 2011-2022 走看看