重复的字符字符串N次R中
在这篇文章中,我们将讨论如何重复N次在R编程语言。表示的字符集。
例子:
“Hello Geek”,”Python”,”Languages_Python” are some examples
方法一:使用replicate()方法
用来给n这个函数从复制品
句法:
replicate(N, “string”)
在哪里,
- N 是字符串被复制的次数
- 字符串是输入
实施例:R t程序使用复制重复N次
R
# get 2 times
print(replicate(2, "Hello_Geek"))
print("-----")
# get 10 times
print(replicate(10, "Python"))
print("-----")
# get 3 times
print(replicate(3, "java"))
print("-----")
# get 4 times
print(replicate(4, "dbms"))
print("-----")
# get 5 times
print(replicate(5, "sql"))
print("-----")
# get 7 times
print(replicate(7, "big data"))
R
# get 2 times
print(rep("Hello_Geek", 2))
print("-----")
# get 10 times
print(rep("Python", 10))
print("-----")
# get 3 times
print(rep("java", 3))
print("-----")
# get 4 times
print(rep("dbms", 4))
print("-----")
# get 5 times
print(rep("sql", 5))
print("-----")
# get 7 times
print(rep("big data", 7))
R
# get 2 times with delimiter --
paste(replicate(2, "Geek"), collapse = "--")
print("-----")
# get 10 times with delimiter ,
paste(replicate(2, "Python"), collapse = ",")
R
# get 2 times
print(strrep( "Hello_Geek",2))
print("-----")
# get 10 times
print(strrep( "Python",10))
print("-----")
# get 3 times
print(strrep( "java",3))
print("-----")
# get 4 times
print(strrep("dbms",4))
print("-----")
# get 5 times
print(strrep( "sql",5))
print("-----")
# get 7 times
print(strrep("big data",7))
输出:
[1] “Hello_Geek” “Hello_Geek”
[1] “—–“
[1] “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”
[9] “Python” “Python”
[1] “—–“
[1] “java” “java” “java”
[1] “—–“
[1] “dbms” “dbms” “dbms” “dbms”
[1] “—–“
[1] “sql” “sql” “sql” “sql” “sql”
[1] “—–“
[1] “big data” “big data” “big data” “big data” “big data” “big data” “big data”
方法二:使用 rep() 方法
此函数的工作方式类似于复制。
句法:
rep( “string”,N)
实施例:R t程序重复字符N次使用Rep
电阻
# get 2 times
print(rep("Hello_Geek", 2))
print("-----")
# get 10 times
print(rep("Python", 10))
print("-----")
# get 3 times
print(rep("java", 3))
print("-----")
# get 4 times
print(rep("dbms", 4))
print("-----")
# get 5 times
print(rep("sql", 5))
print("-----")
# get 7 times
print(rep("big data", 7))
输出:
[1] “Hello_Geek” “Hello_Geek”
[1] “—–“
[1] “Python” “Python” “Python” “Python” “Python” “Python” “Python” “Python”
[9] “Python” “Python”
[1] “—–“
[1] “java” “java” “java”
[1] “—–“
[1] “dbms” “dbms” “dbms” “dbms”
[1] “—–“
[1] “sql” “sql” “sql” “sql” “sql”
[1] “—–“
[1] “big data” “big data” “big data” “big data” “big data” “big data” “big data”
方法 3:使用粘贴和复制
此粘贴用于以正确的方式组织重复的字符串,它将使用给定的分隔符分隔字符串。
句法:
paste(replicate(N, “string”), collapse = “delimiter”)
在哪里,
- paste 用于显示数据
- 重复用于获取N个
- 折叠被起诉分离字符串
实施例:R t程序使用粘贴命令重复的
电阻
# get 2 times with delimiter --
paste(replicate(2, "Geek"), collapse = "--")
print("-----")
# get 10 times with delimiter ,
paste(replicate(2, "Python"), collapse = ",")
输出:
[1] “Geek–Geek”
[1] “—–“
[1] “Python,Python”
方法四:使用 strrep()函数
此函数是用来获取N个在一个字符串。
句法:
strrep( “string”,N)
实施例:R t程序使用strrep()函数得到N个
电阻
# get 2 times
print(strrep( "Hello_Geek",2))
print("-----")
# get 10 times
print(strrep( "Python",10))
print("-----")
# get 3 times
print(strrep( "java",3))
print("-----")
# get 4 times
print(strrep("dbms",4))
print("-----")
# get 5 times
print(strrep( "sql",5))
print("-----")
# get 7 times
print(strrep("big data",7))
输出:
[1] “Hello_GeekHello_Geek”
[1] “—–“
[1] “PythonPythonPythonPythonPythonPythonPythonPythonPythonPython”
[1] “—–“
[1] “javajavajava”
[1] “—–“
[1] “dbmsdbmsdbmsdbms”
[1] “—–“
[1] “sqlsqlsqlsqlsql”
[1] “—–“
[1] “big databig databig databig databig databig databig data”