zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Clojure 学习入门(4)- 字符串

学习入门 字符串 Clojure
2023-09-14 09:16:11 时间
一、创建  
函数str: 函数 (str) 接受任意数量的参数。如果参数不是字符串则将其转换为字符串,返回创建的新字符串。如果没有参数或为nil,则返回空字符串""
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=>   
  2. user=> (str 1)  
  3. "1"  
  4. user=> (str -2.5)  
  5. "-2.5"  
  6. user=> (str "a")  
  7. "a"  
  8. user=> (str "abc" 123)  
  9. "abc123"  
  10. user=> (str)  
  11. ""  
  12. user=> (str '(1 2 3))  
  13. "(1 2 3)"  
  14. user=> (str nil)  
  15. ""  
  16. user=> (str null)  
  17. java.lang.Exception: Unable to resolve symbol: null in this context (NO_SOURCE_FILE:149)  
  18. user=> (str "null")  
  19. "null"  
  20. user=> (str "abc " 123)  
  21. "abc 123"  
  22. user=> (str 123 345)  
  23. "123345"  
其它比较少用的创建函数 
print-str、println-str、pr-str、prn-str、with-out-str 
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (print-str "abc" 234)  
  2. "abc 234"  
  3. user=> (println-str "abc" 234)  
  4. "abc 234\n"  
  5. user=> (pr-str "abc" 234)  
  6. "\"abc\" 234"  
  7. user=> (prn-str "abc" 234)  
  8. "\"abc\" 234\n"  
  9. user=> (with-out-str "abc" 234)  
  10. ""  

二、string操作 
count函数: count函数接受字符串参数时,返回字符串的字符数。 
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (count)  
  2. java.lang.IllegalArgumentException: Wrong number of args (0) passed to: core$count (NO_SOURCE_FILE:175)  
  3. user=> (count nil)  
  4. 0  
  5. user=> (count "abc123")  
  6. 6  

subs函数: subs函数接受两个或三个参数, 第一个是字符串,第二个是一个整数偏移量,第三个(可选)是另一个整数偏移量。函数返回从第一个偏移量(含),到第二个(不含)偏移量或者结尾(如果没有第二个偏移量)截取的子字符串。
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (subs "ithomer" 1)  
  2. "thomer"  
  3. user=> (subs "ithomer" 1 3)  
  4. "th"  
  5. user=> (subs "ithomer" 1 (count "ithomer"))  
  6. "thomer"  
  7. user=> (subs "ithomer" 1 20)  
  8. java.lang.StringIndexOutOfBoundsException: String index out of range: 20 (NO_SOURCE_FILE:0)  
  9. user=> (subs "ithomer")  
  10. java.lang.IllegalArgumentException: Wrong number of args (1) passed to: core$subs (NO_SOURCE_FILE:0)  

format函数: format函数用于格式化字符串。使用jdk的java.util.Formatter类完成格式化。 
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (format "hello %s" "ithomer.net")  
  2. "hello ithomer.net"  
  3. user=> (format "%5d" 3)  
  4. "    3"  
  5. user=> (format "%-5d" 3)  
  6. "3    "  
  7. user=> (format "%05d" 3)  
  8. "00003"  

检查函数:字符串检查函数 (string?) 接受一个参数,如果是字符串返回true,否则返回false 
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (string? "abc")  
  2. true  
  3. user=> (string? "123")  
  4. true  
  5. user=> (string? 123)  
  6. false  
  7. user=> (string? nil)  
  8. false  

字符检查函数(char?)接受一个参数,如果是字符类型返回true,否则返回false 
[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. user=> (char? "abc")  
  2. false  
  3. user=> (char? \a)  
  4. true  
  5. user=> (char? 'a')  
  6. java.lang.Exception: Unmatched delimiter: )  
  7. user=> (char? a)  
  8. java.lang.Exception: Unable to resolve symbol: a in this context (NO_SOURCE_FILE:222)  
  9. user=> (char? 1)  
  10. false  
  11. user=> (char? \1)  
  12. true  
  13. user=> (char? nil)  
  14. false