你可以使用適當(dāng)?shù)木幊陶Z言將行號添加到文本中。以下是在Python和JavaScript中完成此任務(wù)的一些示例。
在Python中:
```python
text = '''Hello
World
This
Is
A
Test
'''
lines = text.split('\n')
lines_with_numbers = ["{0} {1}".format(i, line) for i, line in enumerate(lines, start=1)]
text_with_line_numbers = '\n'.join(lines_with_numbers)
print(text_with_line_numbers)
```
在上述Python腳本中,我們首先將文本分割成不同的行,然后使用 enumerate 函數(shù)為每一行添加一個索引號,最后,我們將所有行連接起來。
在JavaScript中:
```javascript
let text = `Hello
World
This
Is
A
Test`;
let lines = text.split('\n');
let linesWithNumbers = lines.map((line, index) => `${index+1} ${line}`);
let textWithLineNumbers = linesWithNumbers.join('\n');
console.log(textWithLineNumbers);
```
在上述JavaScript腳本中,我們也是首先將文本分割成行,然后使用 map 函數(shù)為每一行添加一個索引號,最后,我們將所有行連接起來。
以上示例將會對文本的每一行添加行號,然后打印出添加行號的文本。