惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - Franky
V2EX - 技术
V2EX - 技术
V
V2EX
T
Tailwind CSS Blog
P
Privacy International News Feed
S
Securelist
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
月光博客
月光博客
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Simon Willison's Weblog
Simon Willison's Weblog
C
Check Point Blog
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
L
LangChain Blog
The Hacker News
The Hacker News
Project Zero
Project Zero
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
Latest news
Latest news
S
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
C
Cisco Blogs
I
Intezer
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
I
InfoQ
Engineering at Meta
Engineering at Meta
博客园 - 司徒正美
小众软件
小众软件
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
TaoSecurity Blog
TaoSecurity Blog

博客园 - zqf620

.Net PetShop 3.0中购物车总价计算的bug .NET PetShop 3.0 FAQ novalidate选项无效的问题 .NET方向高级开发人员面试时应该事先考虑的问题 (zt) UML中的图 从一组数中每次抽取出一个数,并规定了每个数出现的概率 从一个表中随即抽取100条记录 PL/SQL User's Guide and Reference, Release 2 (9.2) chm版 下载 Oracle中实现自动增长列 在开发过程中运用UML 在DataGrid控件中编辑数据项 在DataGrid控件中获取数据项中各列的数据内容 DataGrid控件的分页 DTD简介 W3C XML Schema (XSD) XML相关技术概览 将web窗体页文件(test.aspx)转换成用户控件文件(test.ascx) access作为后台数据库遇到的访问权限问题 HTML实体
输出到html页面的字符串的格式化
zqf620 · 2007-01-20 · via 博客园 - zqf620

当使用Response.Write()函数将字符串输出到html页面时候,因为html的默认实体的问题,有时候输出并不是预期的那样。比如:
Response.Write("hi      tom");  //字符串中间有六个空格
在web页面的显示却是: hi tom //html自动将连续的空格合并为一个

要达到预期的效果,必须象下面这样:
Response.Write("hi      tom");

这样显得很繁琐,你可以写一个函数来自动帮你将" "换成;&nbsp。代码如下:
------------------------------------------------------------------
public string FormatString(string str)
{
 str=str.Replace(" "," ");
 str=str.Replace("<","&lt;");
 str=str.Replace(">","&gt;");
 str=str.Replace('\n'.ToString(),"<br>");
 return str;
}
------------------------------------------------------------------

这样,要输出"hi      tom"的话,可以写成:
-------------------------------------
string str1 = "hi      tom" ;
Respone.Write(FormatString(str));
-------------------------------------
比如,下面的语句:
------------------------------------------------
string str1 = "Hi , Tom\nHi , Jim\n<===>";
Response.Write(FormatString(str1));
------------------------------------------------
在web页面上的输出为:
Hi , Tom
Hi , Jim
<===>

当然,你可以为这个函数扩充更多的功能。