R语言【cli】——ansi_substr():ANSI彩色字符串的子字符串

Package cli version 3.6.0


Description

这是base::substr()的颜色感知副本。它的工作原理与原来的完全相同,但保留了子字符串中的颜色。在计算字符串中的位置时,忽略ANSI转义序列。


Usage

ansi_substr(x, start, stop)

Arguments

参数【x】:字符向量,可能是ANSI样式的,或者是强制转换为字符的向量。

参数【start】:起始索引或索引,循环以匹配参数【x】的长度。

参数【stop】:结束索引或索引,循环以匹配参数【x】的长度。


Value

长度与参数【x】相同的字符向量,包含请求的子字符串。ANSI样式被保留。


Examples

> str <- paste(
+     col_red("red"),
+     "default",
+     col_green("green")
+ )
> cat(str, "\n")
red default green 
> cat(ansi_substr(str, 1, 5), "\n")
red d 
> cat(ansi_substr(str, 1, 15), "\n")
red default gre 
> cat(ansi_substr(str, 3, 7), "\n")
d def 
> substr(ansi_strip(str), 1, 5)
[1] "red d"
> substr(ansi_strip(str), 1, 15)
[1] "red default gre"
> substr(ansi_strip(str), 3, 7)
[1] "d def"
> str2 <- paste(
+     "another",
+     col_red("multi-", style_underline("style")),
+     "text"
+ )
> cat(str2, "\n")
another multi-style text 
> cat(ansi_substr(c(str, str2), c(3,5), c(7, 18)), sep = "\n")
d def
her multi-styl
> substr(ansi_strip(c(str, str2)), c(3,5), c(7, 18))
[1] "d def"          "her multi-styl"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALittleHigh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值