stringbuilder

Advanced formatting rules for StringBuilder.AppendFormat

蹲街弑〆低调 提交于 2019-12-03 20:21:47
I've seen on this site a StringBuilder code sample illustrating AppendFormat usage: using System; using System.Text; class Program { static int[] _v = new int[] { 1, 4, 6 }; static void Main() { StringBuilder b = new StringBuilder(); foreach (int v in _v) { b.AppendFormat("int: {0:0.0}{1}", v, Environment.NewLine); } Console.WriteLine(b.ToString()); } } === Output of the program === int: 1.0 int: 4.0 int: 6.0 Where can I find documentation on those advanced rules for string formatting? Composite Formatting Standard Numeric Format Strings Custom Numeric Format Strings Standard Date and Time

Is it required to check before replacing a string in StringBuilder (using functions like “Contains” or “IndexOf”)?

蓝咒 提交于 2019-12-03 16:29:38
问题 Is there any method IndexOf or Contains in C#. Below is the code: var sb = new StringBuilder(mystring); sb.Replace("abc", "a"); string dateFormatString = sb.ToString(); if (sb.ToString().Contains("def")) { sb.Replace("def", "aa"); } if (sb.ToString().Contains("ghi")) { sb.Replace("ghi", "assd"); } As you might have noticed I am using ToString() above again and again which I want to avoid as it is creating new string everytime. Can you help me how can I avoid it? 回答1: If the StringBuilder

How to maxmise the largest contiguous block of memory in the Large Object Heap

女生的网名这么多〃 提交于 2019-12-03 15:33:41
The situation is that I am making a WCF call to a remote server which is returns an XML document as a string. Most of the time this return value is a few K, sometimes a few dozen K, very occasionally a few hundred K, but very rarely it could be several megabytes (first problem is that there is no way for me to know). It's these rare occasions that are causing grief. I get a stack trace that starts: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Xml.BufferBuilder.AddBuffer() at System.Xml.BufferBuilder.AppendHelper(Char* pSource, Int32 count)

StringBuilder append() and null values

谁都会走 提交于 2019-12-03 14:37:29
问题 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

Read all ini file values with GetPrivateProfileString

核能气质少年 提交于 2019-12-03 12:17:59
问题 I need a way to read all sections/keys of ini file in a StringBuilder variable: [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); ... private List<string> GetKeys(string iniFile, string category) { StringBuilder returnString = new StringBuilder(255); GetPrivateProfileString(category, null, null, returnString, 32768, iniFile); ... } In returnString is

Write StringBuilder to Stream

北慕城南 提交于 2019-12-03 10:27:16
问题 What is the best method of writing a StringBuilder to a System.IO.Stream? I am currently doing: StringBuilder message = new StringBuilder("All your base"); message.Append(" are belong to us"); System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.Text.ASCIIEncoding encoding = new ASCIIEncoding(); stream.Write(encoder.GetBytes(message.ToString()), 0, message.Length); 回答1: Don't use a StringBuilder, if you're writing to a stream, do just that with a StreamWriter: using (var

What is the simplest way to write the contents of a StringBuilder to a text file in .NET 1.1?

假如想象 提交于 2019-12-03 10:27:12
问题 I have to use StringBuilder instead of a List of string because of being stuck with .NET 1.1 for this project. It's really cramping my style, and making me feel like I'm wearing raggedy old leopard skin duds and dragging a lumpy club through the dirt. Anyway, I want to write a series of debug msgs I've written to a file to study at my leisure, as there is too much to see on the screen (the MessageBox doesn't have scrollbars). Some of the easy ways to write a file don't seem to be available in

Java Socket 编程原理及教程

你离开我真会死。 提交于 2019-12-03 10:10:51
对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket。服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了。首先ServerSocket将在服务端监听某个端口,当发现客户端有Socket来试图连接它时,它会accept该Socket的连接请求,同时在服务端建立一个对应的Socket与之进行通信。这样就有两个Socket了,客户端和服务端各一个。 对于Socket之间的通信其实很简单,服务端往Socket的输出流里面写东西,客户端就可以通过Socket的输入流读取对应的内容。Socket与Socket之间是双向连通的,所以客户端也可以往对应的Socket输出流里面写东西,然后服务端对应的Socket的输入流就可以读出对应的内容。下面来看一些服务端与客户端通信的例子: 1、客户端写服务端读 服务端代码 public class Server { public static void main(String args[]) throws IOException { //为了简单起见,所有的异常信息都往外抛 int port = 8899; //定义一个ServerSocket监听在端口8899上 ServerSocket server = new ServerSocket(port); /

String or StringBuilder return values?

浪子不回头ぞ 提交于 2019-12-03 10:02:54
If I am building a string using a StringBuilder object in a method, would it make sense to: Return the StringBuilder object, and let the calling code call ToString()? return sb; OR Return the string by calling ToString() myself. return sb.ToString(); I guess it make a difference if we're returning small, or large strings. What would be appropriate in each case? Thanks in advance. Edit: I don't plan on further modifying the string in the calling code, but good point Colin Burnett. Mainly, is it more efficient to return the StringBuilder object, or the string? Would a reference to the string get

Trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB

被刻印的时光 ゝ 提交于 2019-12-03 09:43:47
What is an efficient way to trim whitespace from the end of a StringBuilder without calling ToString().Trim() and back to a new SB new StringBuilder(sb.ToString().Trim()) . Nicholas Petersen The following is an extension method, so you can call it like this: sb.TrimEnd(); Also, it returns the SB instance, allowing you to chain other calls ( sb.TrimEnd().AppendLine() ). public static StringBuilder TrimEnd(this StringBuilder sb) { if (sb == null || sb.Length == 0) return sb; int i = sb.Length - 1; for (; i >= 0; i--) if (!char.IsWhiteSpace(sb[i])) break; if (i < sb.Length - 1) sb.Length = i + 1;