stringbuilder

Split string after certain character count

二次信任 提交于 2019-11-28 12:50:21
I need some help. I'm writing an error log using text file with exception details. With that I want my stack trace details to be written like the below and not in straight line to avoid the user from scrolling the scroll bar of the note pad or let's say on the 100th character the strings will be written to the next line. I don't know how to achieve that. Thanks in advance. SAMPLE(THIS IS MY CURRENT OUTPUT ALL IN STRAIGHT LINE) STACKTRACE: at stacktraceabcdefghijklmnopqrstuvwxyztacktraceabcdefghijklmnopqrswxyztacktraceabcdefghijk * *MY DESIRED OUTPUT (the string will write to the next line

Memory Leak in StringBuilder when using Insert() and Clear()

六月ゝ 毕业季﹏ 提交于 2019-11-28 12:32:52
I need to add some lines to a StringBuilder, where the line added last should be at the beginning of the string and the oldest at the end. I add a new line like this: stringBuilder.Insert(0, "Some text." + Environment.NewLine); Once done, I empty the StringBuilder like this: stringBuilder.Clear(); I reuse the same StringBuilder many times, which leads to an Out of Memory exception over time. I investigated the problem with the test program below, which shows that when using Append() and Clear(), the Capacity of the StringBuilder stays the same, regardless how many times it is used. But when

How many spaces does \t use in c#

て烟熏妆下的殇ゞ 提交于 2019-11-28 12:17:28
问题 I am building a report using StringBuilder and the details of it to be properly intended and aligned for which i will be using private static int paperWidth = 55; //it defines the size of paper private static readonly string singleLine = string.Empty.PadLeft(paperWidth, '-'); StringBuilder reportLayout; reportLayout.AppendLine("\t" + "Store Name"); I want Store Name in center and many such more feilds by use of \t Thanks in Advance. EDIT I want to print like. Store Name in center 回答1: If you

Why doesn't StringBuilder have IndexOf method?

丶灬走出姿态 提交于 2019-11-28 10:47:46
I understand that I can call ToString().IndexOf(...), but I don't want to create an extra string. I understand that I can write a search routine manually. I just wonder why such a routine doesn't already exist in the framework. Unfortunately, many of the methods implemented for String could have been implemented for StringBuilder but that was not done. Consider using extension methods to add what you care about. I know this is an old question, however I have written a extension method that performs an IndexOf on a StringBuilder . It is below. I hope it helps anyone that finds this question,

Efficient way to replace chars in a string (java)?

て烟熏妆下的殇ゞ 提交于 2019-11-28 10:35:40
I'm writing a small JAVA program which: takes a text as a String takes 2 arrays of chars What im trying to do will sound like "find and replace" but it is not the same so i thought its important to clear it. Anyway I want to take this text, find if any char from the first array match a char in the text and if so, replace it with the matching char (according to index) from the second char array. I'll explain with an example: lets say my text (String) is: "java is awesome!"; i have 2 arrays (char[]): "absm" and "!@*$". The wished result is to change 'a' to '!' , 'b' to '@' and so on.. meaning

StringBuilder to Array of Strings how to convert

扶醉桌前 提交于 2019-11-28 09:54:45
问题 cant anything find or read about this problem ( it's problem only for me but maybe someone know way to fix it. i have function which reading from asset folder public String[] loadFromAsset() throws IOException { String TEMPBUFFER = null; String[] temp; temp = new String[60]; int tempc = 0; BufferedReader bReader = new BufferedReader(new InputStreamReader(cont.getAssets().open("myquests.txt"))); String line="";// = bReader.readLine(); StringBuilder sb = new StringBuilder(); while (line != null

Regex replacements inside a StringBuilder

一个人想着一个人 提交于 2019-11-28 09:42:00
I'm writing the contents of a text file to a StringBuilder and I then want to perform a number of find/replace actions on the text contained in the StringBuilder using regular expressions. I've run into a problem as the StringBuilder replace function is not capable of accepting regular expression arguments. I could use Regex.Replace on a normal string but I'm under the impression that this is inefficient due to the fact that two copies of the string will need to be created in memory as .net strings are immutable. Once I've updated the text I plan to write it back to the original file. What's

学习2____String,StringBuilder,StringBuffer

别等时光非礼了梦想. 提交于 2019-11-28 08:51:49
一:String: String类不可变的原因:底层是由private final char [ ]修饰的字符数组,这也决定了String类不可被继承(final修饰); 1.构造方法:由于在内存中存放的方式不同(常量池,堆),String可以使用常量进行赋值。 //两种方式的构造方法: String str="123"; //引用类型,但可以向常量一样赋值,因为在内存中存放的方式不一样 String str =new String ("123"); 2.常用方法: charAt(index); //获得相应索引下标的字符 length(); //返回该字符数组的大小, 数组用 length属性 String用length()方法, 集合类用size()方法; contains(str); //返回boolean,是否包含相应子串; getBytes(); //返回字节数组; toCharArray(); //转换成字符数组; indexOf(char); //找寻指定元素在string中第一次出现的位置; lastIndexOf(char); //找寻所给元素最后一次出现的位置; repalce(char,char); //替换指定的字符; split(string); //按照string进行字符串的分割,结果是一个str数组; subString(int): 截串

Fastest search method in StringBuilder

泄露秘密 提交于 2019-11-28 07:39:20
I have a StringBuilder named stb_Swap_Tabu used to store Course's Names, I am using the following method to find a course: stb_Swap_Tabu.ToString.Contains("CourseName") in my case, Performance is the most important issue. Is there any faster way? StringBuilder wasn't really intended for all string purposes. If you really need to search one, you have to write your own method. There are several string-searching algorithms suited to different cases. The following is a simple implementation of the Knuth–Morris–Pratt algorithm that only cares about ordinal matches (no case-folding, no culture

Why does the String.intern() method return two different results? [duplicate]

旧街凉风 提交于 2019-11-28 07:19:27
问题 This question already has answers here : Why are the results of of str == str.intern() for these strings different? (4 answers) Closed 28 days ago . I have the code like this: String str1 = new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern() == str1); //true String str2 = new StringBuilder("ja").append("va").toString(); System.out.println(str2.intern() == str2); //false String str3 = new StringBuilder("Str").append("ing").toString(); System.out.println(str3