当前位置: 首页 > 知识库问答 >
问题:

根据条件替换字符串中的字符[重复]

商正诚
2023-03-14

我最近在CodeWars中遇到了这个问题

给定一个数字串,您应该用“0”替换5以下的任何数字,用“1”替换5及以上的任何数字。返回结果字符串。注意:输入永远不会是空字符串

我来自 Python 背景,我们可以使用 if 语句进行索引来解决这个特定问题,如下所示:

def bin(x):
    newstr = ""
    for num in x:
        if int(num) < 5:
            newstr += "0"
        else:
            newstr += "1"
    return newstr

但当我尝试在Swift中使用索引(如Python风格)时,我失败了……经过一段时间的研究,我发现了这个方法:<code>String。replacingOccurrences(共:,带:)

因此,如果我对上述问题的Swift代码是这样的:

func fakeBin(digits: String) -> String {
  
  for i in digits{
    
    if (Int(i)<5){
      some code to replace the char with '0'
    }
    else{some code to replace the char with '1'}
  
    return digits
  }

如何在代码中使用上述方法 String.replacingOccurrences(of: , with: ) 并实现所需的输出?如果这是不可能的,我们还能怎么做?

注意:我已经在网站上查找了解决方案

共有2个答案

施晗日
2023-03-14

您的代码等效物是:

func fakeBinBasicForLoop(digits: String) -> String {
    var output = ""
    for aChar in digits {
        if let intValue = Int(String(aChar)) { //It's a number
            if intValue < 5 {
                output.append("0")
            } else {
                output.append("1")
            }
        } else { //Not a number
            output.append(aChar)
        }
    }
    return output
}

我正在检查它是否是整数,以防万一。

手动,使用 替换出现次数(of:with:):

func fakeBinReplacingOccurences(digits: String) -> String {
    var output = digits
    output = output.replacingOccurrences(of: "0", with: "0") //No really needed in this case, we can ommit it
    output = output.replacingOccurrences(of: "1", with: "0")
    output = output.replacingOccurrences(of: "2", with: "0")
    output = output.replacingOccurrences(of: "3", with: "0")
    output = output.replacingOccurrences(of: "4", with: "0")
    output = output.replacingOccurrences(of: "5", with: "1")
    output = output.replacingOccurrences(of: "6", with: "1")
    output = output.replacingOccurrences(of: "7", with: "1")
    output = output.replacingOccurrences(of: "8", with: "1")
    output = output.replacingOccurrences(of: "9", with: "1")
    return output
}

问题是它每次都会迭代所有字符串,因此您将有 9 个循环。Allso,如果你从替换5,6,7,8,9(即相反的顺序)开始,你的较高值将被替换为1,然后替换为0。顺序很重要。

使用 reduce(into:_:)的替代解决方案:

func fakeBinReduceInto(digits: String) -> String {

    let output = digits.reduce(into: "") { partialResult, character in
        guard let intValue = Int(String(character)) else { partialResult.append(character); return }
        if intValue < 5 {
            partialResult.append("0")
        } else {
            partialResult.append("1")
        }
    }
    return output
}

也可以按照其他答案的建议使用< code>map()。

蓝苗宣
2023-03-14

Swift<code>字符串

func fakeBin(digits: String) -> String {
    return String(digits.map { $0 < "5" ? "0" : "1" })
}
 类似资料:
  • 我有以下任务。有一根绳子。我必须按照6条规则在它中做替换,直到有可能在一个字符串中做一个替换。 我找到的解决办法就在下面。工作正常。问题是它的性能较低。我还可以如何根据多个规则进行替换?有什么算法吗? 附注。此任务来自codility站点。我的解决方案得到了100%的正确性和25%的性能。

  • 给定一个包含项a和B的字符串M,我想用每个a来代替B,用每个B来代替a来代替M'。天真地,人们会尝试用B代替A,然后用A代替B,但在这种情况下,M'只包含A。我可以想到替换这些项,并记录它们的位置,这样这些项就不会再被替换了。当我们只有A和B可以替换时,这是有效的。但是如果我们需要替换两个以上的项,而且它们的长度不同,那么这就变得很棘手了。 所以我想这样做: 我们将M作为输入字符串,并将R=[(x

  • 如何用单个字符替换n/重复字符?所有特殊字符

  • 假设我有一个字符串,如下所示: 我想把“abcd”换成“dddd”。我曾试图做这样的事: 它不起作用。有什么建议吗? 编辑:更具体地说,我在Java工作,我试图解析超文本标记语言文档,具体地说

  • 本文向大家介绍替换Java字符串中的子字符串,包括了替换Java字符串中的子字符串的使用技巧和注意事项,需要的朋友参考一下 假设以下是我们的字符串。 我们想将子字符串“ Dead”替换为“ Alive”。为此,让我们使用以下逻辑。在这里,我们使用了while循环,并在其中找到了要替换的子字符串的索引。这样,我们一个接一个地替换了整个子字符串。 以下是替换子字符串的完整示例。 示例 输出结果

  • 问题内容: 我正在寻找一种替换文件中的字符串而不将整个文件读入内存的方法。通常我会使用Reader和Writer,例如以下内容: 但是,我想就地进行替换,并且不认为可以同时在同一文件上打开Reader和Writer。另外,我正在使用Java 1.4,因此无法访问NIO,扫描仪等。 谢谢唐 问题答案: 通常无法对文件进行“就地”替换,除非替换的长度与原始长度完全相同。否则,文件将需要增大,从而将所有