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 创建表和定义数据的 ...
随机推荐
- The BINARY and VARBINARY Types
mysql> CREATE TABLE t (c BINARY()); Query OK, rows affected (0.21 sec) mysql> INSERT INTO t SE ...
- TraceGL监控Node.js应用或者浏览器JavaScript代码
https://github.com/traceglMPL/tracegl TraceGL能够监控Node.js应用或者浏览器JavaScript代码的运行过程和细节.可视化的用户界面也很友好
- [转] npm 模块安装机制简介
npm 是 Node 的模块管理器,功能极其强大.它是 Node 获得成功的重要原因之一. 正因为有了npm,我们只要一行命令,就能安装别人写好的模块 . $ npm install 本文介绍 npm ...
- JS 拼凑字符串
和Java一样,JS中直接用"+"号拼凑字符串是很耗费资源的,所以在大量拼凑字符串的情景中,我们也需要一个类似于StringBuffer的工具, 下面利用Array.join()方 ...
- Python建立socket并获取信息
import socket, sys port = 80 host = "www.baidu.com" print "Creating socket..." s ...
- Maven Integration for Eclipse 正确地址
m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...
- nopi导入导出
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- oracle命令的缩写原型单词方便记忆总结
$ORACLE_HOME/bin下的utilities解释 Binary First Available Description adapters ...
- oracle-行转列
<一>合并两个结果集,并且两个结果集的数据 根据条目自动归为一行结果集1 如下:SQL> select t1.fplx,t1.djje from yw_zjfpjl t1 ; FP ...
- C# var
VAR 是3.5新出的一个定义变量的类型其实也就是弱化类型的定义VAR可代替任何类型编译器会根据上下文来判断你到底是想用什么类型的 至于什么情况下用到VAR 我想就是你无法确定自己将用的是什么类型就可 ...