◆ 如何用c#修改保存指定的字符串? ◆

yahg111 2005-09-18 07:02:49
比如下面某文本的多行字符串:

"
one澳大利亚-墨尔本/one
two美国-加州/two
three法国-巴黎/three
"
问:
1.如何实现将第二行的字符附值给一个string的变量?
2.如何实现将one /one 之间的字符付值给一个string的变量?


...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yahg111 2005-09-18
  • 打赏
  • 举报
回复
谢谢
wuyi8808 2005-09-18
  • 打赏
  • 举报
回复
using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string s0 =
@"
one澳大利亚-墨尔本/one
two美国-加州/two
three法国-巴黎/three
";
string [] split = Regex.Split(s0, @"\r\n");
string s1 = split[1]; // <------- split[1]就是原字符串第2行
Console.WriteLine(s1);
Match m = Regex.Match(s1, @"one(.*)/one");
string s2 = m.Groups[1].ToString(); // <------- Groups[1]就是one../one之间的
Console.WriteLine(s2);
}
}

/* 程序输出:
one澳大利亚-墨尔本/one
澳大利亚-墨尔本
*/
wuyi8808 2005-09-18
  • 打赏
  • 举报
回复
1.如何实现将第二行的字符附值给一个string的变量?
-------------------------------------------------

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string s0 =
@"
one澳大利亚-墨尔本/one
two美国-加州/two
three法国-巴黎/three
";
string [] split = Regex.Split(s0, @"\r\n");
Console.WriteLine(split[1]); // <------------ split[1]就是你所要的
}
}

/* 程序输出:
one澳大利亚-墨尔本/one
*/
yahg111 2005-09-18
  • 打赏
  • 举报
回复
可不可以具体点呢
碼上道 2005-09-18
  • 打赏
  • 举报
回复
楼主可以使用正则表达式!

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧