stringbuilder

Best way to remove the last character from a string built with stringbuilder

那年仲夏 提交于 2019-12-03 04:06:44
问题 I have the following data.AppendFormat("{0},",dataToAppend); The problem with this is that I am using it in a loop and there will be a trialling comma. What is the best way to remove the trailing comma? Do I have to change data to a string the substring it? 回答1: The simplest and most efficient way is to perform this command: data.Length--; by doing this you move the pointer (i.e. last index) back one character but you don't change the mutability of the object. In fact, clearing a

When do you use StringBuilder.AppendLine/string.Format vs. StringBuilder.AppendFormat?

女生的网名这么多〃 提交于 2019-12-03 04:02:39
问题 A recent question came up about using String.Format(). Part of my answer included a suggestion to use StringBuilder.AppendLine(string.Format(...)). Jon Skeet suggested this was a bad example and proposed using a combination of AppendLine and AppendFormat. It occurred to me I've never really settled myself into a "preferred" approach for using these methods. I think I might start using something like the following but am interested to know what other people use as a "best practice": sbuilder

C# 遍历控件名称

主宰稳场 提交于 2019-12-03 03:48:15
List<string> list = new List<string>(); list.Add("textBox2"); list.Add("textBox1"); list.Add("txtNum"); StringBuilder sb = new StringBuilder(); foreach (Control ctl in this.Controls) { if (ctl is TextBox) { //txtNum var obj = ctl.Name; sb.AppendFormat("list.Add(\"{0}\");",obj); } } var name = sb.ToString(); 来源: https://www.cnblogs.com/enych/p/11775342.html

Jersey LoggingFilter with log4j

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a rest service developed with jersey, I have a ContainerRequestFilters for print the request, as follows: <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value> </init-param> and I have logger in the post methods using log4j. But the LoggingFilter print in the log different log4j. Is there any way that LogginFilter use the log4j's configuration? I tried this in the log4j.xml file: <logger name="com.sun.jersey.api.container

C# Performance - Chunking Write of file with AppendAllText

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a more elegant/faster way to write the code below? Currently taking about 45seconds. query.sql is 200,000 lines long and has SQL in it exactly like this on each line: SELECT N'+dave' AS [AccountName], N'20005' AS [EmployeeID], N'-6' AS [PlatformID] UNION ALL I found that by chunking into blocks of 1000 that things were much quicker than waiting until the end and using WriteAllText (which took about 20 minutes to run) static void Main(string[] args) { var s = new Stopwatch(); s.Start(); string textToWrite = ""; string[] lines = File

StringBuilder append() and null values

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a list of String s, and I want to concatenate them with spaces in between. So I'm using StringBuilder . Now if any of the String s are null , they get stored in the StringBuilder literally as 'null'. Here is a small program to illustrate the issue: public static void main(String ss[]) { StringBuilder sb = new StringBuilder(); String s; s = null; System.out.println(sb.append("Value: ").append(s)); } I'd expect the output to be "Value: " but it comes out as "Value: null" Is there a way around this problem? 回答1: You can do a check on the

StringBuilder - Reset or create a new

匿名 (未验证) 提交于 2019-12-03 02:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a condition that a StringBuilder keeps storing lines matching a pattern from a large flat file (100's of MB). However after reaching a condition I write the content of the StringBuilder varialble to a textfile. Now I wonder if I should use the same variable by resetting the object -> stringBuilder.delete(0,stringBuilder.length()) OR stringBuilder=new StringBuilder(); Please suggest which would do you think is better as far as both performance and OOM issues are concerned. 回答1: I think StringBuilder#delete(start, end) is still

String concatenation with Groovy

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber ( bank , branch , checkDigit , account ) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber ( bank , branch , checkDigit , account ) { "$bank$branch$checkDigit$account" } I've founded an interesting point about this topic in the old Groovy website: Things you can do but better leave undone. As in Java, you can concatenate Strings with the "+" symbol. But Java only needs that one of the two items of a "+"

Performance issues with nested loops and string concatenations

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can someone please explain why this code is taking so long to run (i.e. >24 hours): The number of rows is 5000, whilst the number of columns is 2000 (i.e. Approximately 10m loops). Is there a better way to do this???? for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { textToWrite += m[i, j].ToString() + ","; } //remove the final comma. textToWrite = textToWrite.Substring(0,textToWrite.Length-2); textToWrite += Environment.NewLine; } 回答1: Because you are creating tons of strings. You should use StringBuilder for this.

从字节码角度看String、StringBuffer、StringBuilder的不同

被刻印的时光 ゝ 提交于 2019-12-03 02:38:00
Oracle官方说明: javap 将一个或多个类文件进行分解。 使用简要说明 javap [options] classfile... options 命令行选项,详细查看后面的Options介绍 classfile 一个或多个Class文件(多个使用空格分离),可以使用文件路径或者classPath下的文件或者输入URL Description javap命令分解卸一个或多个类文件。输出取决于所使用的选项。当没有使用任何选项,那么javap命令打印方案为protected和公共字段和方法。javap命令将输出打印到控制台。 Options -help --help -? 打印帮助信息 -version 打印版本信息 -l 打印行内变量以及局部变量 -public 显示public访问修饰的内容 -protected 显示protected、public访问修饰的内容 -private -p 显示所有的内容 -Joption 将指定的选项传递给JVM。例如: javap -J-version javap -J-Djava.security.manager -J-Djava.security.policy=MyPolicy MyClassName For more information about JVM options, see the java command