Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

r - How to create a string with sprintf

I would like to create a string like JSON format using the sprintf function.
I use this code:

elements_list = sprintf('{"id":"%s",
                          "top": "%s",
                          }', 5, 4)

But the result when I print the string is like this:

"{"id":"5","top":"4"}"

with "" character everywhere, any help?

Thanks!

question from:https://stackoverflow.com/questions/65936147/how-to-create-a-string-with-sprintf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work - you don't need all the double-quotes inside the expression:

elements_list = sprintf('{id:%s top: %s}', 5, 4)
elements_list
# [1] "{id:5 top: 4}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...