ios - 如何截断歌曲名称中的 .wav 和下划线
问题描述
我有一个可以正常工作的歌曲播放器,但我不知道如何将 .wav 去掉结尾,并将下划线从歌曲名称中去掉。当我的音乐播放器不工作时,那是因为没有下划线。如果不更改歌曲文件名,我将如何做到这一点。
Secure_The_Bag.wav to Secure The Bag
我认为这段代码会做到这一点,但事实并非如此。
var name = String()
func getName() -> String {
return name
}
专辑歌曲视图控制器:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SongsTableViewCell", for: indexPath) as! SongsTableViewCell
cell.mainLabel.text = song[indexPath.row].getName()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let songName = song[indexPath.row].getName()
let fileURLString = "\(filePath)\(songName)"
print(fileURLString)
Player.shared.playStream(fileUrl: fileURLString)
}
解决方案
有很多方法可以实现您想要的结果。最简单/直接的方法之一是使用该replacingOccurences
方法。
var name = "Secure_The_Bag.wav"
name = name.replacingOccurrences(of: ".wav", with: "")
// name is now "Secure_The_Bag"
name = name.replacingOccurrences(of: "_", with: " ")
// name is now "Secure The Bag"
推荐阅读
- c# - 合并两个列表的重复项时条件合并
- c - 跟踪递归函数
- linux - 随机排序不适用于 --random_source=FILE
- c# - 连接字符串时,C# 中的 for 循环为何如此缓慢?
- javascript - assert.fail(1, 2) 不显示 node.js 10 的弃用警告
- python - 如何使用两个嵌套列表来制作字典列表?
- error-handling - 在 Smalltalk/Pharo 中创建以块为参数的键值消息
- java - 由于缓慢的 IO 调用,Google Dataflow 的工作人员几乎处于空闲状态
- javascript - Typescript Promise 拒绝类型
- svg - SVG 旋转(矩阵)矩形的宽度。看起来像宽度和高度数字被缩放