首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

clojure.string

完整名称空间名称:clojure.string

概述

代码语言:javascript
复制
Clojure String utilities

It is poor form to (:use clojure.string). Instead, use require
with :as to specify a prefix, e.g.

(ns your.namespace.here
  (:require [clojure.string :as str]))

Design notes for clojure.string:

1. Strings are objects (as opposed to sequences). As such, the
   string being manipulated is the first argument to a function;
   passing nil will result in a NullPointerException unless
   documented otherwise. If you want sequence-y behavior instead,
   use a sequence.

2. Functions are generally not lazy, and call straight to host
   methods where those are available and efficient.

3. Functions take advantage of String implementation details to
   write high-performing loop/recurs instead of using higher-order
   functions. (This is not idiomatic in general-purpose application
   code.)

4. When a function is documented to accept a string argument, it
   will take any implementation of the correct *interface* on the
   host platform. In Java, this is CharSequence, which is more
   general than String. In ordinary usage you will almost always
   pass concrete strings. If you are doing something unusual,
   e.g. passing a mutable implementation of CharSequence, then
   thread-safety is your responsibility.

公共变量和函数

blank?功能

代码语言:javascript
复制
Usage: (blank? s)
代码语言:javascript
复制
True if s is nil, empty, or contains only whitespace.

在Clojure版本1.2中添加

capitalize函数

代码语言:javascript
复制
Usage: (capitalize s)
代码语言:javascript
复制
Converts first character of the string to upper-case, all other
characters to lower-case.

在Clojure版本1.2中添加

ends-with? 函数

代码语言:javascript
复制
Usage: (ends-with? s substr)
代码语言:javascript
复制
True if s ends with substr.

在Clojure版本1.8中添加

逃逸函数

代码语言:javascript
复制
Usage: (escape s cmap)
代码语言:javascript
复制
Return a new string, using cmap to escape each character ch
from s as follows:

If (cmap ch) is nil, append ch to the new string.
If (cmap ch) is non-nil, append (str (cmap ch)) instead.

在Clojure版本1.2中添加

includes?函数

代码语言:javascript
复制
Usage: (includes? s substr)
代码语言:javascript
复制
True if s includes substr.

在Clojure版本1.8中添加

指数剥离

代码语言:javascript
复制
Usage: (index-of s value)
       (index-of s value from-index)
代码语言:javascript
复制
Return index of value (string or char) in s, optionally searching
forward from from-index or nil if not found.

在Clojure版本1.8中添加

加入函数

代码语言:javascript
复制
Usage: (join coll)
       (join separator coll)
代码语言:javascript
复制
Returns a string of all elements in coll, as returned by (seq coll),
separated by an optional separator.

在Clojure版本1.2中添加

最后一个指数 函数

代码语言:javascript
复制
Usage: (last-index-of s value)
       (last-index-of s value from-index)
代码语言:javascript
复制
Return last index of value (string or char) in s, optionally
searching backward from from-index or nil if not found.

在Clojure版本1.8中添加

下例函数

代码语言:javascript
复制
Usage: (lower-case s)
代码语言:javascript
复制
Converts string to all lower-case.

在Clojure版本1.2中添加

re-quote-replacement函数

代码语言:javascript
复制
Usage: (re-quote-replacement replacement)
代码语言:javascript
复制
Given a replacement string that you wish to be a literal
replacement for a pattern match in replace or replace-first, do the
necessary escaping of special characters in the replacement.

在Clojure版本1.5中添加

替换函数

代码语言:javascript
复制
Usage: (replace s match replacement)
代码语言:javascript
复制
Replaces all instance of match with replacement in s.

match/replacement can be:

string / string
char / char
pattern / (string or function of match).

See also replace-first.

The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.

For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern.  If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument.  See also documentation for
java.util.regex.Matcher's appendReplacement method.

Example:
(clojure.string/replace "Almost Pig Latin" #"\b(\w)(\w+)\b" "$2$1ay")
-> "lmostAay igPay atinLay"

在Clojure版本1.2中添加

replace-first函数

代码语言:javascript
复制
Usage: (replace-first s match replacement)
代码语言:javascript
复制
Replaces the first instance of match with replacement in s.

match/replacement can be:

char / char
string / string
pattern / (string or function of match).

See also replace.

The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.

For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern.  If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument.  See also documentation for
java.util.regex.Matcher's appendReplacement method.

Example:
(clojure.string/replace-first "swap first two words"
                              #"(\w+)(\s+)(\w+)" "$3$2$1")
-> "first swap two words"

在Clojure版本1.2中添加

逆函数

代码语言:javascript
复制
Usage: (reverse s)
代码语言:javascript
复制
Returns s with its characters reversed.

在Clojure版本1.2中添加

分裂函数

代码语言:javascript
复制
Usage: (split s re)
       (split s re limit)
代码语言:javascript
复制
Splits string on a regular expression.  Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.

在Clojure版本1.2中添加

分行线函数

代码语言:javascript
复制
Usage: (split-lines s)
代码语言:javascript
复制
Splits s on \n or \r\n.

在Clojure版本1.2中添加

starts-with?函数

代码语言:javascript
复制
Usage: (starts-with? s substr)
代码语言:javascript
复制
True if s starts with substr.

在Clojure版本1.8中添加

微调函数

代码语言:javascript
复制
Usage: (trim s)
代码语言:javascript
复制
Removes whitespace from both ends of string.

在Clojure版本1.2中添加

trim-newline函数

代码语言:javascript
复制
Usage: (trim-newline s)
代码语言:javascript
复制
Removes all trailing newline \n or return \r characters from
string.  Similar to Perl's chomp.

在Clojure版本1.2中添加

三元函数

代码语言:javascript
复制
Usage: (triml s)
代码语言:javascript
复制
Removes whitespace from the left side of string.

在Clojure版本1.2中添加

微调函数

代码语言:javascript
复制
Usage: (trimr s)
代码语言:javascript
复制
Removes whitespace from the right side of string.

在Clojure版本1.2中添加

上案例函数

代码语言:javascript
复制
Usage: (upper-case s)
代码语言:javascript
复制
Converts string to all upper-case.

在Clojure版本1.2中添加

代码语言:txt
复制
 © Rich Hickey

使用EclipsePublicLicense 1.0授权。

扫码关注腾讯云开发者

领取腾讯云代金券