如何在 Julia 中将字符串的特定字母设为大写?
How do I make a specific letter of a string uppercase in Julia?
如何将字符串的特定字母变为大写,而不更改任何其他字母?
我的例子:
"this works" -> "this woRks" //Change made to letter 7
"this works" -> "this wOrks" //Change made to letter 6
"this works" -> "This works" //Change made to letter 1
我的系统使用 UTF-8 编码的字符,因此它需要支持 UTF-8 字符的大写,而不仅仅是 ascii。
这是如何通过分割字符串来做到这一点的:
在 Julia 0.6 中
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
pos = chr2ind(s, i)
string(s[1:prevind(s, pos)], uppercase(s[pos]), s[nextind(s, pos):end])
end
for i in 1:3
println(uppercasen("kół", i))
end
在 Julia 0.7 中(这里我使用 SubString
因为它比使用 String
快一点——类似的事情可以在 Julia 0.6 中完成)
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
pos = nextind(s, 0, i)
string(SubString(s, 1, prevind(s, pos)), uppercase(s[pos]), SubString(s, nextind(s, pos)))
end
for i in 1:3
println(uppercasen("kół", i))
end
但是,下面的代码应该可以在两个版本的 Julia 下运行(不幸的是它比较慢):
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
io = IOBuffer()
for (j, c) in enumerate(s)
write(io, i == j ? uppercase(c) : c)
end
String(take!(io))
end
未优化的一行 :)
julia> s = "this is a lowercase string"
"this is a lowercase string"
julia> String([i == 4 ? uppercase(c) : c for (i, c) in enumerate(s)])
"thiS is a lowercase string"
如果您使用的是简单的 ASCII 字符串,这里还有另一个片段:
toupper(x, i) = x[1:i-1] * uppercase(x[i:i]) * x[i+1:end]
julia> toupper("this works", 1)
"This works"
julia> toupper("this works", 4)
"thiS works"
julia> toupper("this works", 7)
"this wOrks"
这种方法的一个小优点是它可以简单地改编为
`toupper(x, i, j) = x[1:i-1] * uppercase(x[i:j]) * x[j+1:end]`
将字符串中的 范围 转换为大写,而不是单个字母。
如何将字符串的特定字母变为大写,而不更改任何其他字母?
我的例子:
"this works" -> "this woRks" //Change made to letter 7
"this works" -> "this wOrks" //Change made to letter 6
"this works" -> "This works" //Change made to letter 1
我的系统使用 UTF-8 编码的字符,因此它需要支持 UTF-8 字符的大写,而不仅仅是 ascii。
这是如何通过分割字符串来做到这一点的:
在 Julia 0.6 中
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
pos = chr2ind(s, i)
string(s[1:prevind(s, pos)], uppercase(s[pos]), s[nextind(s, pos):end])
end
for i in 1:3
println(uppercasen("kół", i))
end
在 Julia 0.7 中(这里我使用 SubString
因为它比使用 String
快一点——类似的事情可以在 Julia 0.6 中完成)
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
pos = nextind(s, 0, i)
string(SubString(s, 1, prevind(s, pos)), uppercase(s[pos]), SubString(s, nextind(s, pos)))
end
for i in 1:3
println(uppercasen("kół", i))
end
但是,下面的代码应该可以在两个版本的 Julia 下运行(不幸的是它比较慢):
function uppercasen(s::AbstractString, i::Int)
0 < i <= length(s) || error("index $i out of range")
io = IOBuffer()
for (j, c) in enumerate(s)
write(io, i == j ? uppercase(c) : c)
end
String(take!(io))
end
未优化的一行 :)
julia> s = "this is a lowercase string"
"this is a lowercase string"
julia> String([i == 4 ? uppercase(c) : c for (i, c) in enumerate(s)])
"thiS is a lowercase string"
如果您使用的是简单的 ASCII 字符串,这里还有另一个片段:
toupper(x, i) = x[1:i-1] * uppercase(x[i:i]) * x[i+1:end]
julia> toupper("this works", 1)
"This works"
julia> toupper("this works", 4)
"thiS works"
julia> toupper("this works", 7)
"this wOrks"
这种方法的一个小优点是它可以简单地改编为
`toupper(x, i, j) = x[1:i-1] * uppercase(x[i:j]) * x[j+1:end]`
将字符串中的 范围 转换为大写,而不是单个字母。