恢复字符串插值后的值替换

时间:2019-06-13 18:47:49

标签: swift string string-interpolation

给出一个由"My name is Gustavo"创建的str:"My name is \(foo.name)"

替换值后是否可以获得字符串"My name is \(foo.name)"


换句话说,是否可以找出"My name is \(foo.name)",拥有"My name is Gustavo"


编辑:如果我可以"My name is ",也就是所有动态生成的字符串中的硬编码字符串,那就是解决方案。

1 个答案:

答案 0 :(得分:3)

否,这是不可能的。实际上,from random import shuffle questions = parse_csv('questions.txt') shuffledquestions = list(questions.items()) shuffle(shuffledquestions) for (question, answer) in shuffledquestions: questionno+=1 correct = answers[-1] answers = answers[:-1] print(questionno) result = input(f"{question}: {','.join(answers)}") if result=="A" or result=="B" or result=="C" or result=="D": if result == correct: print('Correct!') score+=1 else: print(f'The correct answer is {correct!r}') break else: print("Invalid Entry") break 甚至不包含在已编译的代码中。相反,Swift-5编译器生成的代码与此等效:

"My name is \(foo.name)"

This article包含有关在Swift-4和Swift-5中如何实现字符串插值的详细信息。