我应该如何从字符串中删除所有前导空格?- 迅速 [英] How should I remove all the leading spaces from a string? - swift

查看:35
本文介绍了我应该如何从字符串中删除所有前导空格?- 迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来从作为空格的字符串中删除第一个字符.我正在寻找一种方法,甚至是 String 类型的扩展,我可以用它来剪切字符串的一个字符.

I need a way to remove the first character from a string which is a space. I am looking for a method or even an extension for the String type that I can use to cut out a character of a string.

推荐答案

要删除前导和尾随空格:

To remove leading and trailing whitespaces:

let trimmedString = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

斯威夫特 3/斯威夫特 4:

Swift 3 / Swift 4:

let trimmedString = string.trimmingCharacters(in: .whitespaces)

这篇关于我应该如何从字符串中删除所有前导空格?- 迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆