“字符不可用”,请直接使用字符串

编程入门 行业动态 更新时间:2024-10-27 17:16:42
本文介绍了“字符不可用”,请直接使用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道如何解决它。我只想了解它的工作原理以及应该替换的内容。

I can't figure out how to fix it. I just want to understand how it works and what should be replaced.

我已经尝试删除个字符。

I've already tried to delete characters., but it still doesn't work.

import Foundation var shrinking = String("hello") repeat { print(shrinking) shrinking = String(shrinking.characters.dropLast()) } while shrinking.characters.count > 0

我希望程序输出:

你好 地狱 hel he h

hello hell hel he h

,但根本不起作用。

推荐答案

您的代码如果删除字符应该可以正常工作,建议您创建一个新的Playground文件。顺便说一句,您可以简单地使用 RangeReplaceableCollection 变异方法 popLast 并在字符串不为空时进行迭代,以避免调用集合计数属性多次:

Your code should work as it is if you delete the characters I suggest you create a new playground file. Btw you can simply use RangeReplaceableCollection mutating method popLast and iterate while your string is not empty to avoid calling your collection count property multiple times:

var shrinking = "hello" repeat { print(shrinking) shrinking.popLast() } while !shrinking.isEmpty

这将打印

你好

hello

地狱

hel

he

h

或使用 removeLast 方法,但它要求字符串不为空,因此您需要在关闭前检查字符串是否为空:

or using removeLast method but it requires the string not to be empty so you would need to check if the string is empty at before the closure:

var shrinking = "hello" while !shrinking.isEmpty { print(shrinking) shrinking.removeLast() }

更多推荐

“字符不可用”,请直接使用字符串

本文发布于:2023-10-30 01:19:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:请直接   字符串   不可用   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!