字符串操作

Golang

// 字符去重排序
func listDropDupSort(str string) string {
	// 利用map去重
	m := make(map[rune]bool)
	for _, v := range str {
		m[v] = true
	}
	// 追加到切片
	s := make([]string, 0, len(m))
	for k, _ := range m {
		s = append(s, string(k))
	}
	// 稳定排序
	sort.Stable(sort.StringSlice(s))
	return strings.Join(s, "")
}
// 字符修改
str := "asdsa"
list := []rune(str)
list[0] = 'c'
str = string(list)

Nodejs

// 字符去重排序
const str = "ajldjlajfdljfddd"
const list = Array.from(new Set(str)).sort()
const s = list.join('')
// 字符修改
let str = "asdsa"
const list = str.split('')
list[0] = 'c'
str = list.join('')

Python

// 字符去重排序
str = "ajldjlajfdljfddd"
list = list(set(str))
list.sort()
s = ''.join(list)
// 字符修改
str = "asdsa"
list = list(str)
list[0] = 'c'
str = ''.join(list)

C#

// 字符去重排序
string str = "ajldjlajfdljfddd";
List<char> list = str.Distinct().ToList();
list.Sort();
var s = string.Join("", list);
// 字符修改
string str = "ajldjlajfdljfddd";
List<char> list = str.ToList();
list[0] = 'c';
str = string.Join("", list);
posted @ 2022-08-27 10:16  codeIsArt  阅读(13)  评论(0编辑  收藏  举报