stringbuilder

string类型的解释与方法

旧时模样 提交于 2019-12-01 09:57:10
基本概念 string(严格来说应该是System.String) 类型是我们日常coding中用的最多的类型之一。那什么是String呢?^ ~ ^ String是一个不可变的连续16位的Unicode代码值的集合,它直接派生自System.Object类型。 与之对应的还有一个不常用的安全字符串类型 System.Security.SecureString ,它会在非托管的内存上分配,以便避开GC的黑手。主要用于安全性特高的场景。[具体可查看msdn这里不展开讨论了。 =>msdn查看详情 特性 由于String类型直接派生于Object,所以它是引用类型,那就意味着String对象的实例总是存在于堆上。 String具有不变性,也就是说一旦初始化,它的值将永远不变。 String类型是封闭的,换言之,你的任何类型不能继承String。 定义字符串实例的关键字 string 只是 System.String 类型的一个映射。 注意事项 关于字符串中的回车符和换行符一般大家喜欢直接硬编码‘\r\n’,但是不建议这么做,一旦程序迁移到其他平台,将出现错误。相反,推荐使用 System.Environment 类的 NewLine 属性来生成回车符和换行符,可以跨平台使用的。 常量字符串的拼接和非常量字符串在CLR中行为是不一样的。具体请查看性能部分。 字符串之前加

Java 性能优化小细节

℡╲_俬逩灬. 提交于 2019-12-01 09:55:54
代码优化的目标是 减小代码的体积 提高代码运行的效率 代码优化细节 1、尽量指定类、方法的final修饰符 带有final修饰符的类是不可派生的。在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是final的。为类指定final修饰符可以让类不可以被继承,为方法指定final修饰符可以让方法不可以被重写。如果指定了一个类为final,则该类所有的方法都是final的。Java编译器会寻找机会内联所有的final方法,内联对于提升Java运行效率作用重大,具体参见Java运行期优化。此举能够使性能平均提高50% 。 2、尽量重用对象 特别是String对象的使用,出现字符串连接时应该使用StringBuilder/StringBuffer代替。由于Java虚拟机不仅要花时间生成对象,以后可能还需要花时间对这些对象进行垃圾回收和处理,因此,生成过多的对象将会给程序的性能带来很大的影响。 3、尽可能使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中速度较快,其他变量,如静态变量、实例变量等,都在堆中创建,速度较慢。另外,栈中创建的变量,随着方法的运行结束,这些内容就没了,不需要额外的垃圾回收。 4、及时关闭流 Java编程过程中,进行数据库连接、I/O流操作时务必小心,在使用完毕后,及时关闭以释放资源

可变的StringBuffer类和StringBuilder类

一笑奈何 提交于 2019-12-01 08:27:42
基本概述 由于String类描述字符序列上是不可改变的,因此描述多个类似的字符串需要单独保存,此时内存空间消耗比较大。 为了节省内存空间直接对对象字符序列本身进行改变,则使用 StrungBuffer和StringBuilder。 1. StringBuffer属于线程安全的类,效率比较低 2. StringBuilder属于非线程安全的类,效率比较高,推介使用 常用方法 StringBuilder(String str) - 根据字符串来构造对象 - 初始容量为:16 + 字符串参数的长度 - 该类没有重写equals()和hashCode()方法,但是重写了toString() 方法 int capacity() - 用于获取当前对象的容量并返回 int length() - 用于返回当前对象的字符个数 StringBuilder insert(int offset, String str) - 用于将str插入到当前字符串中offset的位置并返回。 插入的时候会改变字符串的容量 StringBuilder append(String str) - 用于将str追加到当前字符串的末尾位置 StringBuilder delete(int start, int end) - 用于从当前字符串中将start到end之间的字符串内容移除。 StringBuilder replace

How to do a multiple case insensitive replace using a StringBuilder

落爺英雄遲暮 提交于 2019-12-01 08:09:26
问题 I have a (large) template and want to replace multiple values. The replacement needs to be done case insensitive. It must also be possible to have keys that does not exist in the template. For example: [TestMethod] public void ReplaceMultipleWithIgnoreCaseText() { const string template = "My name is @Name@ and I like to read about @SUBJECT@ on @website@, tag @subject@"; const string expected = "My name is Alex and I like to read about C# on stackoverflow.com, tag C#"; var replaceParameters =

Is there an open-source (non GPL) implementation of a functional version of .NET StringBuilder?

▼魔方 西西 提交于 2019-12-01 07:39:41
问题 I'm looking for a functional (as in, non-imperative) implementation of StringBuilder or equivalent. I've seen a couple of functional arrays implementation, but they don't support insertion natively. Bonus for open-source, non-(L?A?)GPL, bonus for F#, but I can translate from Haskell/OCaml/SML if needed. Suggestions for algorithms welcome. 回答1: StringBuilder s advantage over string is due to minimizing allocations. It pre-allocates a buffer to avoid allocating for every insert/append. This

Java字符/字符串

家住魔仙堡 提交于 2019-12-01 07:23:18
字符 Character类用于对单个字符进行操作。Character类在对象中包装一个基本类型char的值。 转义序列 转义序列 描述 \t tab键 \b 后退键 \n 换行 \r 回车 \f 换页符 \' 单引号 \" 双引号 \\ 反斜杠 Character方法 方法 描述 isLetter() 是否是一个字母 isDigit() 是否是一个数字字符 isWhitespace() 是否是一个空白字符 isUpperCase() 是否是大写字母 isLowerCase() 是否是小写字母 toUpperCase() 指定字母的大写形式 toLowerCase() 指定字母的小写形式 toString() 返回字符的字符串形式,字符串长度仅为1 字符串 在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。 String类提供了11种构造方法。 String类是不可改变的,一旦创建了String对象,那它的值就无法改变了。 可变字符串 当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。 StringBuilder 类在 Java 5 中被提出,它和

h u p u

房东的猫 提交于 2019-12-01 07:14:57
package com.hupu.monitor.net; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class b { private static MessageDigest a; private static StringBuilder b; static { try { a = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException noSuchAlgorithmException) {} b = new StringBuilder(); } public static String a(String paramString) { a.reset(); a.update(paramString.getBytes()); byte[] arrayOfByte = a.digest(); StringBuilder stringBuilder = b; byte b1 = 0; stringBuilder.setLength(0); while (b1 < arrayOfByte.length) { byte b2 = arrayOfByte[b1] & 0xFF

What is StringBuilder's RAM consumption like?

点点圈 提交于 2019-12-01 04:48:30
We have a few operations where we are doing a large number of large string concatenations, and have recently encountered an out of memory exception. Unfortunately, debugging the code is not an option, as this is occurring at a customer site. So, before looking into a overhaul of our code, I would like to ask: what is the RAM consumption characteristics of StringBuilder for large strings? Especially as they compare to the standard string type. The size of the strings are well over 10 MB, and we seem to run into the issues around 20 MB. NOTE : This is not about speed but RAM. Here is a nice

Java基础学习笔记(五) - 常用的API

╄→гoц情女王★ 提交于 2019-12-01 02:58:16
API介绍 概念:API 即应用编程程序接口。Java API是JDK中提供给我们使用的类说明文档,这些类将底层的代码实现封装。无需关心这些类是如何实现,只需要学习如何使用。 使用:通过API找到需要使用的类,学习使用构造方法和成员方法。(创建对象,调用即可) 一、Scanner类 功能:解析基本类型和字符串的简单文本扫描器。 构造方法:无参构造 Scanner,生成的值是从指定输入流扫描的。 成员方法: nextInt 方法会将输入信息记录为int类型。 import java.util.Scanner; //导包 public class TestScanner { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("正在录入数据:"); int a = sc.nextInt(); System.out.println(a); } } System.in 表示通过键盘录入数据,然后使用 Scanner类的 nextInt 方法获取输入数据。 使用导包:使用 import 关键字,引入包中的类。java.lang 包下的类无需导入。 二、Random类 功能:生成伪随机数。 构造方法:无参构造随机数生成器。 成员方法: nextInt 方法

What is StringBuilder's RAM consumption like?

橙三吉。 提交于 2019-12-01 02:42:19
问题 We have a few operations where we are doing a large number of large string concatenations, and have recently encountered an out of memory exception. Unfortunately, debugging the code is not an option, as this is occurring at a customer site. So, before looking into a overhaul of our code, I would like to ask: what is the RAM consumption characteristics of StringBuilder for large strings? Especially as they compare to the standard string type. The size of the strings are well over 10 MB, and