Replicate String in C#
My original posting on string repetition caused a couple responses, and is currently among the Top Posts, which indicates to me that this seems to be a frequent and non-trivial problem.
The .Net API requires that we need to handle two different cases:
Replicating Chars
If you need to replicate a character value, reader Chris points out correctly that the string constructor
s = new string(’*', count);
is to be used.
Replicating Strings
I found the code used in the original posting to replicate an array and convert it to a string afterwards somewhere on the web. What I did not like abound that one-liner was that it allocated first the array, and then the string, resulting in double the memory which should be necessary.
Reader StewartFip posted the StringBuilder.Insert method as a solution, which seems to do the job:
new StringBuilder().Insert(0,”myString”,count).ToString()
Comparison
My guess was that allocating enough memory in the StringBuilder constructor should speed up the Insert(). Actually, it was NOT!
A small benchmarking application showed consistently, that the fastest way is to use StringBuilder.Insert(), a couple percent faster than new StringBuilder(totalsize).Insert(), and the array-to-string conversion taking twice as long.
Replicate String in C#的更多相关文章
- 2008技术内幕:T-SQL语言基础
2008技术内幕:T-SQL语言基础 单表查询摘记 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLF ...
- C++ Style Languages: C++, Objective-C, Java, C#
Hyperpolyglot.org From Hyperpolyglot.org C++ Style Languages: C++, Objective-C, Java, C# a side-by-s ...
- 2008技术内幕:T-SQL语言基础 单表查询摘记
这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLFundamentals2008 ,官网给出的连接是这 ...
- SQL-字符串运算符和函数
COALESCE(columnname,string) 函数 将 NULL 值作为字符串(用空字符串或其他字符串替换 NULL)- 接受一列输入值(字段)如果该字段为 NULL,则返回后面替换的字符串 ...
- SQLSERVER字符串处理函数
sqlserver提供了一系列字符串处理函数:substring.left.right.len.charindex.patindex.replace.replicate.stuff.upper.low ...
- SQL查询和编程基础
本文转自http://www.cnblogs.com/Jolinson/p/3552786.html 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基 ...
- day35-hibernate映射 05-Hibernate的一级缓存:快照区
SessionImpl里面有很多的Java集合,很多java集合才构成了一级缓存.一级缓存里面有一个非常特殊的区域叫做快照区.SessionImpl实现了Session接口,有很多Java集合(包括M ...
- T-SQL语言基础(转载)
本文转自http://www.cnblogs.com/Jolinson/p/3552786.html 这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基 ...
- SQL Server2012 T-SQL基础教程--读书笔记(1-4章)
SQL Server2012 T-SQL基础教程--读书笔记(1-4章) SqlServer T-SQL 示例数据库:点我 Chapter 01 T-SQL 查询和编程背景 1.3 创建表和定义数据的 ...
随机推荐
- 银行B2C直连
银行B2C直连: 1. 银联B2C直连流程: 注: 所有银行B2C接入方式均为此流程. 不同银行只是签名及验签方式不一致,请求及返回参数不一样,其他的一致. 2. 银行B2C支付请求: 交易流程: ( ...
- Apache开启Gzip压缩设置(转)
第一步: 查看一下/etc/httpd/conf/httpd.conf中下面两个模块是否开启: LoadModule deflate_module modules/mod_deflate.so Loa ...
- +load,+initialize原理
+load,+initialize原理 1.load 父类的load方法在子类load方法之前调用,分类的load方法在原来类load方法之后调用,依赖类的load方法会在自己之前调用,总之所有的类的 ...
- linux 命令终端提示符显示-bash-4.1#解决方法
昨天在配置linux,突然发现root登录的CRT的终端提示符显示的是-bash-4.1# 而不是root@主机名 + 路径的显示方式.搞了半天也不知道为什么出现这种情况.今天终于搞定这个问题, 原因 ...
- 自己写的demo。List<HashMap<String,Object>>=new ArrayList<HashMap<String,Object>>
package com.pb.collection; import java.util.ArrayList; import java.util.HashMap; import java.util.It ...
- 用java写bp神经网络(二)
接上篇. Net和Propagation具备后,我们就可以训练了.训练师要做的事情就是,怎么把一大批样本分成小批训练,然后把小批的结果合并成完整的结果(批量/增量):什么时候调用学习师根据训练的结果进 ...
- IOS学习--UILable使用手册(20150120)
第一步:创建一个UILable对象 UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; 第二步:设置对象的各种属性 ...
- tableView尾部多处一部分空白高度
问题出现的原因:创建tableView时使用的style是UITableViewStylePlain 解决办法: 在创建tableView时,self.automaticallyAdjustsScro ...
- C# div、css
目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...
- Tomcat 8熵池阻塞变慢详解(转)
Tomcat 8熵池阻塞变慢详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: ...