如何使用替换字符串? [英] how to use replace string?

查看:79
本文介绍了如何使用替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换html标签b&从我的文本p和下面的代码,我得到我的文本与html标签的输出。

  public   string  GetArticleText()
{
string htmlStr = ;
string strConnectionString = ConfigurationManager.ConnectionStrings [ ######]的ConnectionString。
SqlConnection thisConnection = new SqlConnection(strConnectionString);
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = ############ ;
thisConnection.Open();
SqlDataReader reader = thisCommand.ExecuteReader();
while (reader.Read())
{
string Name = reader.GetString( 0 );
string Pass = reader.GetString( 1 );

Pass = Pass.Replace( < br>,< span class =code-string>
);
Pass = Pass.Replace( < p> );
Pass = Pass.Replace( < / p> );
htmlStr + = < tr>< h2> +名称+ < / h2>< / tr>< tr>
+传递+ < / tr>;
}

thisConnection.Close();
return htmlStr;
}



客户端:

 <   table     width   =  100%    align   =  center    cellpadding   =  2    cellspacing   =   2    border  < span class =code-keyword> =  0   >  
<% = GetArticleText()%>
< / table >









我有什么遗失的东西或者在上面的代码中做错了?请指教。谢谢。

解决方案

String.Replace 可以工作但你也可以查看 String.Remove 。在您的代码中,您正在从阅读器接收数据,然后替换html。



预防胜于治疗,所以我建议您将其从插入位置移除。从tinymce编辑器中取出内容并将其发送到新的字符串变量并使用下面的方法。



 字符串 HtmlLine =  < / br>; 
字符串 NormalLine = HtmlLine.Replace( < / br> );





或者如果要删除许多html元素

 <   / br  >  

,然后遍历字符串中的字词并删除每次出现的html都在飞行中。您可以使用Select Case或If语句来检查单词中的Html条目,并相应地使用上面提到的方法将其删除。



迭代每个单词 现在不是html ;将每个单词添加到字典中,然后重复字典值以将句子从第一个字典键检索到最后一个字典键。然后,您可以构建不带html的新字符串并将其添加到数据库中。请参阅字典的参考资料。



还有一百个其他以多种方式做到这一点的选择。



最简单的方法是使用If语句来检查html条目,并通过迭代每个单词来删除它们。迭代Google上的每个单词都可以找到很多结果。



 字符串 HtmlLine =  < / br>; 
字符串 NormalLine = HtmlLine.Replace( < / br> );





希望这会有所帮助。如果您有任何疑问,请告诉我们。



如果您觉得这很有帮助,请将其标记为解决方案。


如果< p> 并且其他内容显示在您的浏览器中,则不得在实际字符串中显示。



例如,要获得< p>在我的解决方案中显示,我已输入& lt; p& gt; (& lt; for< and& gt; for>)



如果您的源字符串(在您的数据库中)包含这些字符串,您的浏览器将解释这些字符串并显示解释的字符串。为了确保这一点,你可以查看你的页面的源代码(通过在大多数浏览器中按F12)。



为了纠正你的问题,你可能有替换:

- & lt; p& gt; 而不是< p>

- & lt; / p& gt; 而不是< / p>

但你有很多其他特殊字符串。 (& amp; 例如& 。)


< pre lang =xml> Pass = Pass.Replace(< br > ,);
Pass = Pass.Replace(< p > ,);
Pass = Pass.Replace(< / p > ,);

检查使用的标签< br > < br / >


I am trying to replace the html tags "b" & "p" from my text and with the following code below, I am getting the output of my text with the html tags.

public string GetArticleText()
    {
    string htmlStr = "";
    string strConnectionString = ConfigurationManager.ConnectionStrings["######"].ConnectionString;
    SqlConnection thisConnection = new SqlConnection(strConnectionString);
    SqlCommand thisCommand = thisConnection.CreateCommand();
    thisCommand.CommandText = "############";
    thisConnection.Open();
    SqlDataReader reader = thisCommand.ExecuteReader();
    while (reader.Read())
    {
        string Name = reader.GetString(0);
        string Pass = reader.GetString(1);

        Pass = Pass.Replace("<br>", "");
        Pass = Pass.Replace("<p>", "");
        Pass = Pass.Replace("</p>", "");
        htmlStr += "<tr><h2>" + Name + "</h2></tr><tr>" + Pass + "</tr>";
    }

    thisConnection.Close();
    return htmlStr;
    }


Client-side:

<table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" >
       <%=GetArticleText()%>
   </table>





Is there something i missing or doing incorrectly in my above code? please advice. Thanks.

解决方案

String.Replace will work but you can also look at String.Remove. In your code you are receiving data from a reader, and then replacing the html.

Prevention is better than cure, so I would suggest that you remove it from where you are inserting it from. Take the contents from the tinymce editor and send it to a new string variable and use the method below.

String HtmlLine = "</br>";
String NormalLine = HtmlLine.Replace("</br>", "");



Or If there is lots of html elements to remove

</br>

, then iterate through the words in the string and remove each occurrence of html on the fly. You could use a Select Case or If statement to check for Html entries in the words and remove them using the methods mentioned above accordingly.

While iterating each word that is now not html; add each word to a dictionary, and then reiterate the dictionary values to retrieve your sentence from the first dictionary key to the last dictionary key. You can then construct your new string without html and add it to your database. See a reference of dictionary here.

There are a hundred other options to do this, in many different ways.

The simplest would be to use a If statement to check for html entries and remove them by iterating each word. You can find many results on iterating each word on Google.

String HtmlLine = "</br>";
String NormalLine = HtmlLine.Replace("</br>", "");



Hope this helps. If you have any questions, let me know.

If you found this helpful, please mark it as a solution.


if <p> and others are displayed in your browser, it must not be present as this in the actual string.

For example, to get the <p> displayed in my solution, I have entered &lt;p&gt; ( &lt; for < and &gt; for >)

If your source string (in your database) contains those strings, your browser will interpret those and will display the interpreted string. To be sure of this, you can look at the source code of your page (by pressing F12 in most browser).

And to correct your problem, you may have to replace :
- &lt;p&gt; rather than <p>
- &lt;/p&gt; rather than </p>
but you have a lot of other special strings. (&amp; is & for example.)


Pass = Pass.Replace("<br>", "");
Pass = Pass.Replace("<p>", "");
Pass = Pass.Replace("</p>", "");

Check what tag is used <br> or <br/>


这篇关于如何使用替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆