c# - 使用asp.net c#从字符串中拆分每两个单词
问题描述
我可以使用以下代码从字符串中拆分每个单词:
string s = TextBox1.Text.Trim().ToString(); // a b c d e f g h
string[] words = s.Split(' ');
foreach (string word in words)
{
TextBox1.Text += "\n"+word.ToString();
}
此代码返回输出,如
a
b
c
d
e
f
g
h
我想像这样拆分每两个单词
ab
bc
cd
de
ef
fg
gh
解决方案
您必须将 foreach 转换为 for 循环并使用索引
string s = TextBox1.Text.Trim().ToString(); //a b c d e f g h
string[] words = s.Split(' ');
for (int i = 0; i < words.Length - 1; i++)
{
TextBox1.Text += $"\n{words[i]} {words[i + 1]}";
}
如果我们有“ab c”,它将显示
a b
b c
如果我们有“abc d”
a b
b c
c d
推荐阅读
- sql - 允许插入重复项的合并语句
- python-3.x - Apache Airflow scheduler not running workers on its own
- c++ - 自制 Arduino 莫尔斯电码库在第一个字母后停止
- memory - 试图了解 Apache Arrow 的内存存储是如何工作的
- r - R ggplot2:绘制置信区间色带时出现奇怪的补丁
- c - C中的意外位移行为
- c++ - 无需重载的类型和 const 类型的通用函数
- visual-studio-code - “ANY_NODE_APP”在 Windows 10 上不是内部或外部命令、可运行程序或批处理文件
- sql - 有没有办法使用 SQL 将字符串转换为列名?
- python - 进度条 - 类型错误:“模块”对象不可调用