小编kwo*_*icz的帖子

从字符串中删除所有特殊字符 - ruby

我正在用python挑战来编写ruby中的代码,特别是这个.它在页面源中包含一个非常长的字符串,带有特殊字符.我试图找到一种方法来删除它们/检查字母字符.

我尝试使用扫描方法,但我想我可能不会正确使用它.我也尝试过delete!:

    a = "PAGE SOURCE CODE PASTED HERE"
    a.delete! "!", "@"  #and so on with special chars, does not work(?) 
    a
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢

ruby special-characters

47
推荐指数
5
解决办法
6万
查看次数

淡化代码的问题

我在edx.com上找到了这个课程的代码.有人能告诉我为什么我必须在for循环中使用余数?它如何影响字典?

def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    dict={}
    upper = string.ascii_uppercase
    lower = string.ascii_lowercase
    for l in range(len(upper)):
        dict[upper[l]] = upper[(l+shift)%len(upper)]
    for l in range(len(lower)):
        dict[lower[l]] = lower[(l+shift)%len(lower)]
    return dict
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
113
查看次数

标签 统计

python ×1

ruby ×1

special-characters ×1