83 lines
1.4 KiB
TypeScript
83 lines
1.4 KiB
TypeScript
|
export class StringBuider
|
||
|
{
|
||
|
strings: string[]
|
||
|
|
||
|
constructor()
|
||
|
{
|
||
|
this.strings = []
|
||
|
}
|
||
|
|
||
|
/** 插入 */
|
||
|
Append(obj: any)
|
||
|
{
|
||
|
this.strings.push(obj)
|
||
|
}
|
||
|
|
||
|
/** 插入文本 */
|
||
|
AppendString(str: string) { this.strings.push(str) }
|
||
|
|
||
|
/** 格式化 插入 */
|
||
|
AppendFormat(str: string, ...arg: any[]): string
|
||
|
{
|
||
|
for (let i = 0; i < arg.length; i++)
|
||
|
{
|
||
|
let parent = `\\{${i}\\}`
|
||
|
let reg = new RegExp(parent, 'g')
|
||
|
str = str.replace(reg, arg[i])
|
||
|
}
|
||
|
this.strings.push(str)
|
||
|
return str
|
||
|
}
|
||
|
|
||
|
/** 添加一行(回车) */
|
||
|
AppendLine(str = '')
|
||
|
{
|
||
|
if (str != null)
|
||
|
{
|
||
|
this.AppendString(str)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** 插入 */
|
||
|
Insert(index: number, ...strs)
|
||
|
{
|
||
|
if (strs.length == 0)
|
||
|
return
|
||
|
if (index > this.strings.length)
|
||
|
index = this.strings.length
|
||
|
if (index < 0)
|
||
|
index = 0
|
||
|
this.strings.splice(index, 0, ...strs)
|
||
|
}
|
||
|
|
||
|
/** 删除片段 */
|
||
|
Remove(startIndex: number, length: number): string[]
|
||
|
{
|
||
|
return this.strings.splice(startIndex, length)
|
||
|
}
|
||
|
|
||
|
/** 所有文本 */
|
||
|
ToString(): string
|
||
|
{
|
||
|
return this.strings.join('\r\n')
|
||
|
}
|
||
|
|
||
|
/** 清空 */
|
||
|
Clear() { this.strings = [] }
|
||
|
|
||
|
/** 容量 */
|
||
|
get size() { return this.strings.length }
|
||
|
/** 文本数 */
|
||
|
get Count() { return this.strings.length }
|
||
|
|
||
|
GetStringLength(): number
|
||
|
{
|
||
|
let length = 0
|
||
|
for (const str of this.strings)
|
||
|
{
|
||
|
length += str.length
|
||
|
}
|
||
|
return length
|
||
|
}
|
||
|
}
|