首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将"camelCasing“转换为"camel_casing”

将"camelCasing“转换为"camel_casing”
EN

Stack Overflow用户
提问于 2022-10-15 08:31:48
回答 2查看 39关注 0票数 -1

因此,我编写了一个代码,它可以这样做,但似乎不能将这个thisDoesAThing转换成this_does_a_thing,而是给出一个类似于thisDoesA_thing的输出,所以请帮帮我,这是我的代码:

代码语言:javascript
复制
import string
test = input("Enter your camelCasing")
loop = 1
x = string.ascii_uppercase
m = list(x)
for i in m:
  while True:
    while i in test and loop < 2:
      loop +=1
      n = i.lower()
      j = test.replace(i,"_"+n)
      print(j)
EN

回答 2

Stack Overflow用户

发布于 2022-10-15 08:37:31

这是你要实现的部分。注:不需要嵌套循环。

代码语言:javascript
复制
let output be an empty string

for each symbol in input

   if symbol is uppercase
       unless this is a first symbol, append '_' to the output
       convert symbol to lower case

   append symbol to the output
票数 0
EN

Stack Overflow用户

发布于 2022-10-15 08:42:34

这是@gog建议的python中算法的一个类似的实现。

代码语言:javascript
复制
test = input("Enter your camelCasing")
ans=''
for i in range(len(test)):
    if test[i].isupper():
        if i==0:
            ans+=test[i].lower()
        else:
            ans+="_"+test[i].lower()
    else:
        ans+=test[i]
        
print(ans)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74077912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档