R语言 替换字符串的字符 – chartr() 函数

R语言 替换字符串的字符 – chartr() 函数

R编程语言中的chartr()函数 是用来做字符串替换的。它用作为参数指定的新字符替换一个字符串中所有匹配的现有字符。

语法: chartr(old, new, x)

参数

  • old: 要被替换的旧字符串
  • new: 新字符串
  • x: 目标字符串

例1 :

# R program to illustrate
# chartr function
 
# Initializing a string
x <- "Geeks GFG"
 
# Calling the chartr() function
# which will substitute G with Z
# to the above specified string
chartr("G", "Z", x)
R

输出:

[1] "Zeeks ZFZ"
R

例2 :

# R program to illustrate
# chartr function
 
# Initializing a string
x <- "GeeksforGeeks Geeks"
 
# Calling the chartr() function
# which will substitute G with 1,
# k with 2 and s with 3
# to the above specified string
chartr("Gks", "123", x)
R

输出

[1] "1ee23for1ee23 1ee23"
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册