如何对字符串中的字母进行排序? [英] How to sort letters in a string?

查看:519
本文介绍了如何对字符串中的字母进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串s = "bcabca".

从中提取"aabbcc"的最简单方法是什么,即对s中的字母进行排序?

What is the simplest way to get "aabbcc" out of it, i.e., sort the letters in s?

推荐答案

也许不是最简单的答案,但这会起作用:

Maybe not the most simple answer, but this will work:

paste(sort(unlist(strsplit(s, ""))), collapse = "")

或修改帮助页面中为?strsplit定义的strReverse功能,以适应我们的需求.我们将其称为strSort:

Or modify the strReverse function that is defined in the help page for ?strsplit to suit our needs. We'll call it strSort:

strSort <- function(x)
        sapply(lapply(strsplit(x, NULL), sort), paste, collapse="")

这篇关于如何对字符串中的字母进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆