简体   繁体   English

如何检查一个或多个特定字符是否为字符串?

[英]How do i check if one or more certain characters are an a string?

I want to check if two strings contain "\\n" or "\\r" using just one if statement not two, and is there any way to do this or must I use a for loop? 我想检查两个字符串是否仅使用一个if语句而不是两个包含“ \\ n”或“ \\ r”,并且有任何方法可以做到这一点,还是必须使用for循环?

I tried tweaking the if statement in many ways but it seems like I just cant get it right, so must I use a for loop or two if statements? 我尝试了多种方式来调整if语句,但似乎无法正确处理,因此必须使用for循环还是两个if语句?

def singleline_diff_format(line1, line2, idx):
    """
    Inputs:
      line1 - first single line string
      line2 - second single line string
      idx   - index at which to indicate difference
    Output:
      Returns a three line formatted string showing the location
      of the first difference between line1 and line2.

      If either input line contains a newline or carriage return,
      then returns an empty string.

      If idx is not a valid index, then returns an empty string.
     """
    equals = "=" * (idx)
    min_length = min(len(line1), len(line2))

    if "\n" or "\r" in line1 or line2:
        return ""
    else:
        return line1 +  "\n{}^".format(equals) + "\n" + line2 

print(singleline_diff_format("abcd", "abed", 2))
print(singleline_diff_format("abcd \nhey man", "abed", 2))    
print(singleline_diff_format("abcd", "abed", 5))

I expect 我预计

abcd A B C D

==^ == ^

abed

abcd A B C D

hey man 嗨,老兄

==^ == ^

abed

abcd A B C D

=====^ ===== ^

abed

But I just get empty strings which means that the if statement isn't working properly. 但是我只是得到空字符串,这意味着if语句不能正常工作。

You could use any : 您可以使用任何

if any(char in line for char in ('\n',  '\r') for line in (line1, line2)):
    ....

This will be True if any of the 4 conditions '\\n' in line1 , '\\n' in line2 , '\\r' in line1 , '\\r' in line2 is `True - and is much more compact... 这将是True ,如果任何4个条件中'\\n' in line1'\\n' in line2'\\r' in line1'\\r' in line2是'真-并且更加紧凑...

Your logic isn't doing what you intend and when you do match, you are returning an empty string. 您的逻辑没有按照您的预期进行,当您进行匹配时,您将返回一个空字符串。 if "\\n" or "\\r" in line1 or line2: return "" 如果第1行或第2行中为“ \\ n”或“ \\ r”,则返回“”

Try: if ("\\n" or "\\r") in (line1 or line2): return f'{line1}\\n=\\n{line2}' 尝试:如果(line1或line2)中的(“ \\ n”或“ \\ r”):返回f'{line1} \\ n = \\ n {line2}'

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

相关问题 如何检查文件中的字符串是否包含某些字符? - How do I check if a string in a file contains certain characters? 在Python中一定数量的字符出现多于一次之后,如何修剪字符串? - How do I trim a string after certain amount of characters appear more then once in Python? 我如何检查每个字符串的 2 项是否不止一次出现 - How do i check the 2 item of every string for more than one occurence 如何从字符串中某些行的末尾删除 4 个字符? - How do I delete 4 characters from the end of certain lines in a string? 如何检查某些字符是否不在字符串中? - How to check if certain characters aren't in a string? 如何检查字符串是否包含字符和空格(空字符)? - How do I check if a string includes characters and space (empty character)? 如何检查字符串是否仅包含字母数字字符和短划线? - How do I check if a string only contains alphanumeric characters and dashes? 如何替换字符串中的多个字符 Python - How do I replace more than one character in a string Python 我如何使用正则表达式获取两个字符内的字符串并删除该字符串内的某些字符 - how do i use regex to get a string inside two character and remove certain characters inside that string 如何从python中的字符串中提取由某些字符组成的子字符串? - How do i extract a sub string which is composed of certain characters from a string in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM