如何提取字符串中双引号的内容?

innorence 2010-03-07 01:09:03
<a href="aaa">
类似上面的字符串如果不转义的话是不能赋值到变量的
那怎么才能获取aaa呢?
...全文
1467 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cshava 2010-03-08
  • 打赏
  • 举报
回复
        static string[] GetContent(string strArg)
{
bool isin = false;//是否在引号内
System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>();//储存各引号里的字符串
string current = string.Empty;
foreach (char cur in strArg)
{
if (cur == '"')
{
if (isin)
{
result.Add(current);
}
else
{
current = string.Empty;
}
isin = !isin;
}
else
{
if (isin)
{
current += cur;
}
}
}
return result.ToArray();
}
abaochen 2010-03-08
  • 打赏
  • 举报
回复
indexof 定位第一个引号
再使用INDEXOF 起始位是第一个引号的位置
cshava 2010-03-08
  • 打赏
  • 举报
回复
字符串中双引号用转义符\"表示

要用<a href="aaa">复制可以写成string astr = "<a href=\"aaa\">";
innorence 2010-03-08
  • 打赏
  • 举报
回复
谢谢回复!但是现在问题是带有双引号的字符串没法赋值给变量strArg啊,后边就没法操作~~
引用 12 楼 cshava 的回复:
C# codestaticstring[] GetContent(string strArg)
{bool isin=false;//是否在引号内 System.Collections.Generic.List<string> result=new System.Collections.Generic.List<string>();//储存各引号里的字符串st?-
wuyq11 2010-03-07
  • 打赏
  • 举报
回复
MatchCollection mc = Regex.Matches(yourStr, @"(?is)(?<=(?:href)=(['""]?))[^""'\s>]+(?=\1)");
foreach (Match m in mc)
{
str+ m.Value + "\n";
}
string webDocContent="":
string strPattern=@"a[\s]+href=(? <Link>[^\s>]+)[^>]*>(? <Text>[^ <]*) </a>";
MatchCollection Matches=Regex.Matches(webDocContent,strPattern,RegexOptions.IgnoreCase|RegexOptions.Compiled);
foreach(Match mc in Matches)
{
Response.Write(mc.Groups["Link"].Value.ToString().Trim());
}
innorence 2010-03-07
  • 打赏
  • 举报
回复
正则表达式看过一点都忘光了,看不懂哦
引用 9 楼 wuyq11 的回复:
MatchCollection mc = Regex.Matches(yourStr, @"(?is)(? <=(?:href)=(['""]?))[^""'\s>]+(?=\1)");
foreach (Match m in mc)
{
    str+ m.Value + "\n";
}
string  webDocContent="":
string strPattern=@"a[\s]+href=(? <Link>[^\s>]+)[^>]*>(? <Text>[^ <]*) </a>";
            MatchCollection Matches=Regex.Matches(webDocContent,strPattern,RegexOptions.IgnoreCase|RegexOptions.Compiled);
            foreach(Match mc in Matches)
            {                             
              Response.Write(mc.Groups["Link"].Value.ToString().Trim());
            }
innorence 2010-03-07
  • 打赏
  • 举报
回复
这个我会,但是这个只能自己给字符串添加转义字符,如果需要处理的内容多的话怎么办?那要累死人啊!还是谢谢你和楼下那位o(∩_∩)o
引用 7 楼 nanmosiam 的回复:
你可以这样试下,还有其他方法的,首先要转义字符出来了
C# codeprivatevoid OutSpecial()
{string strtmp="\"strtmp\"";//MessageBox.Show(strtmp);string[] strTmps= strtmp.Split('"');if (strTmps.Length>1)
{
MessageBox.Show(strTmps[1]);
}
}
小生我怕怕 2010-03-07
  • 打赏
  • 举报
回复
string strtmp = "\"strtmp\"";
//MessageBox.Show(strtmp);
string[] strTmps = strtmp.Split('"');
if (strTmps.Length > 1)
{
MessageBox.Show(strTmps[1]);
}
nanmosiam 2010-03-07
  • 打赏
  • 举报
回复
你可以这样试下,还有其他方法的,首先要转义字符出来了

private void OutSpecial()
{
string strtmp = "\"strtmp\"";
//MessageBox.Show(strtmp);
string[] strTmps = strtmp.Split('"');
if (strTmps.Length > 1)
{
MessageBox.Show(strTmps[1]);
}
}
波导终结者 2010-03-07
  • 打赏
  • 举报
回复
(?<=").*(?=")

正则试试
lbkmaster 2010-03-07
  • 打赏
  • 举报
回复
indexof 定位第一个引号
再使用INDEXOF 起始位是第一个引号的位置
innorence 2010-03-07
  • 打赏
  • 举报
回复
引用 3 楼 xray2005 的回复:
最简单的方法,是IndexOf


如果还可以使用正则,正则可问 过客 ,他是最拿手!
能不能给个例子啊?
xray2005 2010-03-07
  • 打赏
  • 举报
回复
最简单的方法,是IndexOf


如果还可以使用正则,正则可问 过客 ,他是最拿手!
innorence 2010-03-07
  • 打赏
  • 举报
回复
双引号里面的内容不确定长度怎么办呢?
引用 1 楼 lbkmaster 的回复:
indexof 定位
lbkmaster 2010-03-07
  • 打赏
  • 举报
回复
indexof 定位

111,125

社区成员

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

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

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