C#代码优化—字符串拼接效率比较
字符串拼接主要有以下几种方法:
- + : 加号
- String.Format() : 字符串格式化
- StringBuilder.Append()
说明
- 对于少量固定的字符串拼接,如string str = "a" + "b" + "c",则csc编译器会优化成 str = String.Concat("a", "b", "c"); 不会创建多个字符串。
如果写成 string str = "a"; s += "b"; s += "c"; 则会多创建3个新字符串。 - String.Format()
用Reflector查看String.Format源码,内部调用的是StringBuilder.public static String Format(
IFormatProvider provider, String format, params Object[] args) {
if (format == null || args == null)
throw new ArgumentNullException((format==null)?"format":"args");
StringBuilder sb = new StringBuilder(format.Length + args.Length * );
sb.AppendFormat(provider,format,args);
return sb.ToString();
} - StringBuider类可以指定内存空间的大小,字符串较少时,可以使用Stirng.Format()代替;
对于少量的字符串操作时可以使用"+"或者String.Format(); 当操作大量字符串时,比如在循环体内,最好使用StringBuider.
C#代码优化—字符串拼接效率比较的更多相关文章
- Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740
Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...
- Java字符串拼接效率测试
测试代码: public class StringJoinTest { public static void main(String[] args) { int count = 10000; long ...
- list 字符串拼接效率实验
ist 字符串拼接有多种方法,我就其中常用三种做了实验,实验代码如下: 第一次是为了初始化静态方法,后面的才是效率比较. 结果如下: StringUtils join 方法用的是StringBuild ...
- python 字符串拼接效率打脸帖
https://www.cnblogs.com/chenjingyi/p/5741901.html 这篇博客写的好,字符串并不是+ 效率就一定比 "%" % ('a') 就低. 按 ...
- Java字符串拼接效率对比
1.来自:http://blog.csdn.net/Zen99T/article/details/51255418 2.来自:http://blog.csdn.net/kimsoft/article/ ...
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...
- 谈JavaScript组合拼接字符串的效率 --转载
JavaScript组合拼接字符串的效率.在脚本开发过程中,经常会按照某个规则,组合拼接出一个大字符串进行输出.比如写脚本控件时控制整个控件的外观的HTML标签输出,比如AJAX里得到服务器端的回传值 ...
- C#的StringBuilder 以及string字符串拼接的效率对照
今天公司一个做Unity3d的人在说字符串拼接的一个效率问题,他觉得string拼接会产生新的一个内存空间,假设不及时回收会产生大量的碎片,特别是在Unity3d这样一个Updata环境下,由于每一帧 ...
- C#三种字符串拼接方法的效率对比
C#字符串拼接的方法常用的有:StringBuilder.+.string.Format.List<string>.使用情况不同,效率不同. 1.+的方式 string sql = &qu ...
随机推荐
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- MFC如何制作字体选择对话框
MFC封装类:CFontDialog 核心代码如下: void CGraphicView::OnFont() { CFontDialog dlg; if( IDOK == dlg.DoModal() ...
- Android -- 获取View宽高
在activity中可以调用View.getWidth.View.getHeight().View.getMeasuredWidth() .View.getgetMeasuredHeight()来获得 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十六)Structured Streaming:WARN clients.NetworkClient: Error while fetching metadata with correlation id 1 : {my-topic=LEADER_NOT_AVAILABLE}
问题描述: 我之前使用kafka的命令删除了改topic: ./kafka-topics.sh --delete --zookeeper [zookeeper server] --topic [to ...
- 解决Ubuntu/debian的Apt-get 由于依赖关系安装失败的问题
The following packages have unmet dependencies: libssl-dev: Depends: libssl0.9.8 (= 0.9.8k-7ubuntu8) ...
- IncrediBuild 2.40 过期时间
IncrediBuild 2.40的License有2个文件CoordLicense.dat和AgentLicense.dat,分别位于Coordinator和Agent安装目录下,这两个文件都是RS ...
- [PureScript] Introduce to PureScript Specify Function Arguments
JavaScript does its error-checking at runtime, but PureScript has a compiler, which makes sure that ...
- 一个十年IT从业者的职场感言:为什么不要自称是“程序员”
转载:https://blog.csdn.net/S_king_/article/details/78529089 如果我可以给每个工程教育增加一门课,它不会涉及编译器.门电路或是时间复杂度,而是一门 ...
- 使用Robot Framework做接口测试
http://chuansong.me/n/1858477 1.RF框架 1.1 RF框架介绍Robot Framework 框架是一个通用的测试框架,一直是由诺西网络(Nokia Siemens N ...
- 【Lua】LuaForWindows_v5.1.4-46安装失败解决方案
下个补丁vcredist_x86.exe 可以到下面连接下载: https://download.csdn.net/download/tvcctv27tv/10344318