StringBuilder.ToString() 在字符串的开头和结尾添加'\'字符.
为什么是这样?
在调用.ToString()之前,字符串没有'\'字符.
我需要创建一个长字符串,大约360个字符,具有我发送给服务的不同值.我知道每个部分的位置,我需要能够插入值和空白,直到我到达下一个位置.以下是我开始的一个例子; 每说我需要从位置0开始,下一个值需要进入位置5(abc).到目前为止,我已经能够连接这样:"1234abc",但我需要的是"1234 [space] abc [space] [space]"感谢您的帮助.
//sbTrialSpaces
private void TrialSpaces()
{
string str1 = "1234";
string str2 = "abc";
string finalStr;//Has to be 10 positions
//like this "1234 abc "
}
Run Code Online (Sandbox Code Playgroud) 我正在读取数百万行的多个文件,并且正在创建具有特定问题的所有行号的列表。例如,如果特定字段保留为空白或包含无效值。
因此,我的问题是,跟踪一百万行以上的数字列表最有效的日期类型是什么。使用字符串生成器,列表或其他方法会更有效吗?
我的最终目标是发出类似“特定字段在1-32、40、45、47、49-51等上为空白的消息。因此对于String Builder,我将检查先前的值以及是否检查它是仅多1个,我会将其从1更改为1-2,如果超过一个,则将其用逗号分隔。使用列表,我只需将每个数字添加到列表中,然后合并,一旦文件包含已被完全阅读,但是在这种情况下,我可能会有多个包含数百万个数字的列表。
这是我正在使用String Builder组合数字列表的当前代码:
string currentLine = sbCurrentLineNumbers.ToString();
string currentLineSub;
StringBuilder subCurrentLine = new StringBuilder();
StringBuilder subCurrentLineSub = new StringBuilder();
int indexLastSpace = currentLine.LastIndexOf(' ');
int indexLastDash = currentLine.LastIndexOf('-');
int currentStringInt = 0;
if (sbCurrentLineNumbers.Length == 0)
{
sbCurrentLineNumbers.Append(lineCount);
}
else if (indexLastSpace == -1 && indexLastDash == -1)
{
currentStringInt = Convert.ToInt32(currentLine);
if (currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Append("-" + lineCount);
else
{
sbCurrentLineNumbers.Append(", " + lineCount);
commaCounter++;
}
}
else if (indexLastSpace > indexLastDash)
{
currentLineSub …Run Code Online (Sandbox Code Playgroud) 我试图用来HashSet删除重复ArrayList<StringBuilder>.
例如,这是一个ArrayList,每一行都是一个StringBuilder对象.
"u12e5 u13a1 u1423"
"u145d"
"u12e5 u13a1 u1423"
"u3ab4 u1489"
Run Code Online (Sandbox Code Playgroud)
我想得到以下内容:
"u12e5 u13a1 u1423"
"u145d"
"u3ab4 u1489"
Run Code Online (Sandbox Code Playgroud)
我目前的实施是:
static void removeDuplication(ArrayList<StringBuilder> directCallList) {
HashSet<StringBuilder> set = new HashSet<StringBuilder>();
for(int i=0; i<directCallList.size()-1; i++) {
if(set.contains(directCallList.get(i)) == false)
set.add(directCallList.get(i));
}
StringBuilder lastString = directCallList.get(directCallList.size()-1);
directCallList.clear();
directCallList.addAll(set);
directCallList.add(lastString);
}
Run Code Online (Sandbox Code Playgroud)
但随着ArrayList规模的扩大,性能变得越来越差.这个实现有什么问题吗?或者你在表现方面有更好的表现吗?
我正在使用类的append()方法遇到一个奇怪的问题StringBuilder; 这是方法:
public StringBuilder toStringBuilder(byte[] b)
{
StringBuilder s = new StringBuilder();
for (int i = 0; i < b.length; i++)
{
s.append(Integer.toString((b[i] & 0xff) + 0x100,16).substring(1));
}
System.out.println(s);
return s;
}
Run Code Online (Sandbox Code Playgroud)
它需要一个字节数组并将其转换为StringBuilder(同时执行其他操作).
一切似乎没问题,当我运行程序时它打印出正确的结果,但也显示了一个NullPointerException不让我继续使用该程序; 例外指向第四行
(for (int i = 0; i < b.length; i++))
Run Code Online (Sandbox Code Playgroud)
这是一个截图:

我试图插入一个字符串,StringBuilder但我得到一个运行时错误:
抛出了类型'System.OutOfMemoryException'的异常.
为什么会发生这种情况?我该如何解决这个问题?
我的代码:
Branch curBranch("properties", "");
foreach (string line in fileContents)
{
if (isKeyValuePair(line))
curBranch.Value += line + "\r\n"; // Exception of type 'System.OutOfMemoryException' was thrown.
}
Run Code Online (Sandbox Code Playgroud)
执行分支
public class Branch {
private string key = null;
public StringBuilder _value = new StringBuilder(); // MUCH MORE EFFICIENT to append to. If you append to a string in C# you'll be waiting decades LITERALLY
private Dictionary <string, Branch> children = new Dictionary <string, Branch>();
public Branch(string nKey, string …Run Code Online (Sandbox Code Playgroud) 我做了一些C#程序,它获取了最新创建的文件.
例如它是一个D:\Directory1\file1.txt 文件
当它获取文件时,其文件的地址存储在某个变量中,如此字符串:
"D:\\Directory1\\file1.txt"
当您在调试中观察时,您可以看到此值.
FileInfo f1; // this is a latest created file "D:\Directory1\file1.txt"
string s1 = f1.FullName; // string value will be "D:\\Directory1\\file1.txt"
string s2 = SomeFunction(s1); // in order to get @"D:\Directory1\file1.txt" value
Run Code Online (Sandbox Code Playgroud)
如果将其值分配给不同的变量s2,那么您将再次获得相同的字符串值!
那么如何将此值赋给不同的变量以获得@"D:\ Directory1\file1.txt"值.
我应该使用StringBuilder类吗?
我应该使用String.Format吗?
还是非常感谢!
我基本上有完全相反的问题
似乎旧的Java Base64实用程序在返回字符串时总是每76个字符添加新行,但使用以下代码,我不会得到我需要的那些中断.
Path path = Paths.get(file);
byte[] data = Files.readAllBytes(path);
String txt= Base64.getEncoder().encodeToString(data);
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以告诉编码器添加换行符?
我已经尝试实现一个stringbuilder来插入换行符,但它最终改变了整个输出(我将文本从java控制台复制到HxD编辑器,并与我已知的工作'BLOB'与换行符进行比较).
String txt= Base64.getEncoder().encodeToString(data);
//Byte code for newline
byte b1 = 0x0D;
byte b2 = 0x0A;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < txt.length(); i++) {
if (i > 0 && (i % 76 == 0)) {
sb.append((char)b1);
sb.append((char)b2);
}
sb.append(txt.charAt(i));
}
Run Code Online (Sandbox Code Playgroud)
编辑(回答问题):
这不是最简单的解释,但是当我不使用字符串构建器时,编码的输出将如下所示:
AAAAPAog4lBVgGJrT2b + mQVicHN3d //////// 3hhcDJiLWVtMjUwLWVtMjUwLWRldjA0NTUAAAAAAA
但我希望它看起来像这样:
AAAAPAog4lBVgGJrT2b + mQVicHN3d //////// 3hhcDJiLWVtMjUwLWVtMjUwLWRldjA0NTUAAAAA..AA
如您所见,".."表示0x0D和0X0A或换行符,它在第76个字符处插入(这是旧base64输出的内容).
但是,当我在第76个字符后追加字节b1和b2(换行符)时,输出变为: …
我试图在一个for循环中插入一个Stringbuilder,但它崩溃了.这是我的代码
StringBuilder sb = new StringBuilder("This is a text");
for(int i = 0;i < sb.length();i++){
if(sb.charAt(i) == 't'){
sb.insert(i, 't');
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的目的是使每个't'加倍.
我在工作中的一些代码中找到了一个部分,其中使用这种格式创建了一堆长字符串:
String s = "" +
"...\n" +
"...\n" +
Run Code Online (Sandbox Code Playgroud)
出于好奇,决定快速测试一下StringBuilder是否会产生明显的变化。
public class TestStringConcat {
public static void main(String[] args) {
int arraySize = 1000000;
String[] strings1 = new String[arraySize];
String[] strings2 = new String[arraySize];
System.out.println("Testing concat version");
long startTime = System.currentTimeMillis();
for (int i = 0; i < arraySize; i++) {
strings1[i] = "" +
"A big long multiline string"; //35 lines of string omitted
}
long endTime = System.currentTimeMillis();
System.out.println("Time to concat strings: " + (endTime - startTime)); …Run Code Online (Sandbox Code Playgroud)