我正在研究需要连接多个字符串的函数。大约200到500个字符串。
我目前正在使用StringBuffer。我想知道这是否是连接多个字符串的最快方法。我需要这种方法尽可能高效。
我在C#中有一个字符串数组,如下所示:
string[] sites = new string[] {
"http://foobar.com",
"http://asdaff.com",
"http://etc.com"
};
Run Code Online (Sandbox Code Playgroud)
我在a中使用此数组,foreach我希望能够添加"类型"值1,2或3,具体取决于我当前正在迭代的站点.我正在与StringBuilder来自这些网站的数据连接.现在,我可以将网站存储为一个varchar,但它会非常整洁,因为这个数组永远不会改变以将数字与字符串相关联并以这种方式构建它.
我在c#中遇到以下错误:此代码"并非所有代码路径都返回值"我正在尝试使用它创建编程语言.任何帮助是极大的赞赏.
private Expr ParseExpr()
{
if (this.index == this.tokens.Count)
{
throw new System.Exception("expected expression, got EOF");
}
if (this.tokens[this.index] is Text.StringBuilder)
{
string Value = ((Text.StringBuilder)this.tokens[this.index++]).ToString();
StringLiteral StringLiteral = new StringLiteral();
StringLiteral.Value = Value;
}
else if (this.tokens[this.index] is int)
{
int intvalue = (int)this.tokens[this.index++];
IntLiteral intliteral = new IntLiteral();
intliteral.Value = intvalue;
return intliteral;
}
else if (this.tokens[this.index] is string)
{
string Ident = (string)this.tokens[this.index++];
Variable var = new Variable();
var.Ident = Ident;
return var;
}
else
{
throw …Run Code Online (Sandbox Code Playgroud) 我对String和String Builder感到困惑.这是我的简单代码
StringBuilder sb1 = new StringBuilder("123");
String s1 = "123";
sb1.append("abc");
s1.concat("abc");
System.out.println(sb1 + " " + s1);
Run Code Online (Sandbox Code Playgroud)
SB1输出123ABC.没关系!因为它使用append方法.但是字符串s1应该是abc123,但它的输出是abc.为什么?什么是concat方法的目的?请解释一下.
谢谢
我有一个StringBuilder,它附加图像中的所有像素,这个数量非常大.每次我运行我的程序时,一切都顺利,但是一旦我改变了像素颜色(ArGB),我得到了一个OutOfMemoryException清除StringBuilder的地方.问题是我需要创建一个实例StreamWriter然后将文本添加到它然后设置文件路径 我目前的代码:
StringBuilder PixelFile = new StringBuilder("", 5000);
Private void Render()
{
//One second run, I get an OutOfMemoryException
PixelFile.Clear();
//This is in a for but cut it out for reverence.
PixelFile.Append(ArGBFormat);
}
Run Code Online (Sandbox Code Playgroud)
我不知道造成这种情况的原因.我曾尝试PixelFile.Length = 0;和PixelFile.Capacity = 0;
我有一个小问题,我的代码工作,并向我发送经度和纬度的输出.我只想用逗号和空格来分隔textview中的值.我不想要行分隔符.
我得到的:32.67543-55.986454
我想要的:32.67543,-55.986454(逗号和空格)
有任何想法吗?
码:
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(JSONObject product) {
if (product != null) {
// product with this pid found
// Edit Text
lblDummyLocation = (TextView) findViewById(R.id.lblDummyLocation);
StringBuilder jsonStringBuilder = new StringBuilder(); //Create StringBuilder for concatenation of JSON results
// display profile data in EditText
try {
//txtMessage.setText(product.getString(TAG_FIRSTNAME)); //Don't set the text here
jsonStringBuilder.append(product.getString(TAG_LATITUDE)); //Concatenate each separate item
jsonStringBuilder.append(System.getProperty("line.separator"));
} catch (JSONException e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个字符串,11 11但我面临的问题是我得到start以下字符串,98 11而不是11 11.
我该如何解决这个问题?
我感谢任何帮助.
Character number = newName.charAt(2); //here number is 1
Character numberBefore = newName.charAt(1); //here numberBefore is 1
try (PrintWriter writer = new PrintWriter(path+File.separator+newName);
Scanner scanner = new Scanner(file)) {
boolean shouldPrint = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(numberBefore >0 ){
String start= number+number+" "+number+number; //here start is `98 11`
}
Run Code Online (Sandbox Code Playgroud) 我有一个StringBuilder,我试图从多个列表中追加参数,如下所示:
var sb = new StringBuilder();
var list1 = new List<string>() { "a", "b", "c" }
var list2 = new List<string>() { "d", "e" }
sb.AppendFormat(" {0}, {1}, {2}, {3}, {4} ", list1, list2);
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
索引(从零开始)必须大于或等于零且小于参数列表的大小.
我做的工作就是创建一个临时列表
var temp = new List<string>();
temp.AddRange(list1);
temp.AddRange(list2);
sb.AppendFormat(" {0}, {1} ,{2} ,{3} ,{4} ", new List().Add);
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来做到这一点?
我正在转换long为String使用String.format().当数字> 0时,它工作正常,但是当我向数字添加前导零时,我在输出中得到一个不同的数字.我的要求是在传递前导零时向用户显示前导零.请指教.
public class CreateContractNumber {
public static void main(String[] args) {
long account = 0000123;
long schedule = 001;
CreateContractNumber ccn = new CreateContractNumber();
System.out.println("CONTRACT #: "+ccn.createContNbr(account, schedule));
}
private String createContNbr(long account, long schedule) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%07d", account);
sb.append("-");
sb.append(String.format("%03d", schedule);
return sb.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
实际产出:合同#:0000083-001
预期产出:合同#:0000123-001
Java中的StringBuffer/ StringBuilderclasses主要用于修改String值,而不必每次都初始化一个新的String对象.
是否有一个特定的原因,它不使用一个LinkedListChar数组作为它的底层数据结构?
将char插入到Array中将始终导致O(n)时间将所有元素复制到下一个索引,而在o(1)时间内将其复制到a LinkedList.
stringbuilder ×10
java ×6
c# ×4
string ×4
.net ×2
arrays ×2
android ×1
comma ×1
exception ×1
function ×1
java-me ×1
json ×1
linked-list ×1
midp ×1
stringbuffer ×1