import Foundation
struct ThreadContent{
let threadNum: Int
let threadTitle: String
let threadPostNum: Int
}
func splitTitle(threadString: String) async -> ThreadContent {
var threadContent: ThreadContent
var colonIndex: String.Index?
var openParenIndex: String.Index?
var number: Int = 0
var title: String = ""
var postCount: Int = 0
// 番号を取り出す(最初のコロンの左側)
if let index = threadString.firstIndex(of: ":") {
colonIndex = index
if let colonIndex = colonIndex {
number = Int(threadString[..<colonIndex]) ?? 0
print("番号: \(number)")
}
}
if let index = threadString.lastIndex(of: "(") {
// タイトルを取り出す(最初のコロンの右が始点、末尾から最初の開き括弧の左が終点)
openParenIndex = index
if let colonIndex = colonIndex, let openParenIndex = openParenIndex {
title = String(threadString[threadString.index(after: colonIndex)..<openParenIndex])
print("タイトル: \(title)")
}
// 投稿数を取り出す(開き括弧右から末尾1文字を削除した数字)
if let openParenIndex = openParenIndex {
let postCountString = threadString[threadString.index(after: openParenIndex)..<threadString.index(before: threadString.endIndex)]
postCount = Int(postCountString) ?? 0
print("投稿数: \(postCount)")
}
}
threadContent = ThreadContent(threadNum: number, threadTitle: title, threadPostNum: postCount)
return threadContent
}