简体   繁体   English

Jython / JES中的大写字母

[英]Capital letters in Jython/JES

I am currently writing a JES program that returns True or False dependent on whether a string containing a palindrome is passed to it. 我目前正在编写一个JES程序,该程序根据是否将包含回文的字符串传递给它来返回True或False。 Although the program works, it fails when a capital letter or punctuation symbol is present. 尽管该程序可以运行,但是当出现大写字母或标点符号时,它将失败。 How could I get it to work? 我该如何运作?

print(ThisPalindrome("racecar"))

>> True 

print(ThisPalindrome("Racecar"))

>> False

To solve the issue with capitalization, you could try using the str.lower() method in your checks. 要解决大写问题,您可以尝试在检查中使用str.lower()方法。

def ThisPalindrome(word):
    lowercase = word.lower()
    reversedOrder = reversed(lowercase)
    if lowercase == ''.join(reversedOrder):
        return True
    else:
        return False

In theory, this function should work with basic punctuation too, as long as it doesn't break the function. 从理论上讲,只要不破坏该功能,该功能也应使用基本标点符号。 Input such as ' could cause it to break. 诸如'输入可能会导致其中断。

The toLowerCase() method to return the calling string value converted to lowercase. toLowerCase()方法返回转换为小写的调用字符串值。

The replace() method to return a new string with some or all matches of a pattern replaced by a replacement. replace()方法返回一个新字符串,该字符串具有部分或全部匹配的模式,并由替换项替换。 We will use one of the RegExp we just created earlier. 我们将使用我们刚才创建的RegExp之一。

The split() method splits a String object into an array of strings by separating the string into sub strings. split()方法通过将String对象拆分为子字符串,将String对象拆分为字符串数组。

The reverse() method reverses an array in place. reverse()方法将数组反转到位。 The first array element becomes the last and the last becomes the first. 第一个数组元素变为最后一个,最后一个数组变为第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM