相关疑难解决方法(0)

C#:最可读的字符串连接.最佳实践

可能重复:
我应该如何连接字符串?

有几种方法可以Concat的字符串在日常任务时的表现并不重要.

  • result = a + ":" + b
  • result = string.Concat(a, ":", c)
  • result = string.Format("{0}:{1}", a, b);
  • StringBuilder approach
  • ......?

你更喜欢什么?为什么效率无关紧要,但你想保持代码最符合你的口味?

c# string string-concatenation

14
推荐指数
2
解决办法
3339
查看次数

为什么'new_file + = line + string'比'new_file = new_file + line + string'快得多?

当我们使用时,我们的代码需要10分钟来虹吸68,000条记录:

new_file = new_file + line + string
Run Code Online (Sandbox Code Playgroud)

但是,当我们执行以下操作时,只需1秒钟:

new_file += line + string
Run Code Online (Sandbox Code Playgroud)

这是代码:

for line in content:
import time
import cmdbre

fname = "STAGE050.csv"
regions = cmdbre.regions
start_time = time.time()
with open(fname) as f:
        content = f.readlines()
        new_file_content = ""
        new_file = open("CMDB_STAGE060.csv", "w")
        row_region = ""
        i = 0
        for line in content:
                if (i==0):
                        new_file_content = line.strip() + "~region" + "\n"
                else:
                        country = line.split("~")[13]
                        try:
                                row_region = regions[country]
                        except KeyError:
                                row_region = "Undetermined"
                        new_file_content += …
Run Code Online (Sandbox Code Playgroud)

python string cpython string-concatenation python-internals

8
推荐指数
2
解决办法
1108
查看次数

C#将字符串添加到另一个字符串

可能重复:
使用C#的最佳字符串连接方法是什么?

我有一个变量:

string variable1;
Run Code Online (Sandbox Code Playgroud)

而我正试图做这样的事情:

for (int i = 0; i < 299; i += 2)
        {
            variable1 = variable1 && IntToHex(buffer[i]);
        }
Run Code Online (Sandbox Code Playgroud)

IntToHex是一个字符串函数,因此"IntToHex(buffer [i])"的结果将是字符串.但它出现了一个错误,说我不能使用&&.有没有解决方案将字符串添加到另一个字符串?谢谢!

c#

7
推荐指数
1
解决办法
3万
查看次数

创建串联字符串的性能

哪种创建字符串的方式在运行时效率更高 C#

1号:

bool value = true;
int channel = 1;
String s = String.Format(":Channel{0}:Display {1}", channel, value ? "ON" : "OFF");
Run Code Online (Sandbox Code Playgroud)

2号:

bool value = true;
String channel = "1";
string  s = ":Channel" + channel + ":Display " + value ? "ON" : "OFF";
Run Code Online (Sandbox Code Playgroud)

c# string performance

7
推荐指数
2
解决办法
1217
查看次数

处理阵列时我应该固定什么?

我正在尝试编写一个DynamicMethod来包装cpblk IL 操作码。我需要在 x64 平台上复制字节数组块,这应该是最快的方法。Array.CopyBuffer.BlockCopy这两个工作,但我想探索的所有选项。

我的目标是将托管内存从一个字节数组复制到一个新的托管字节数组。我关心的是我如何知道如何正确“固定”内存位置。我不希望垃圾收集器移动数组并破坏所有内容。到目前为止它有效,但我不确定如何测试这是否是 GC 安全的。

// copying 'count' bytes from offset 'index' in 'source' to offset 0 in 'target'
// i.e. void _copy(byte[] source, int index, int count, byte[] target)

static Action<byte[], int, int, byte[]> Init()
{
    var dmethod = new DynamicMethod("copy", typeof(void), new[] { typeof(object),typeof(byte[]), typeof(int), typeof(int),typeof(byte[]) },typeof(object), true);
    var il = dmethod.GetILGenerator();

    il.DeclareLocal(typeof(byte).MakeByRefType(), true);
    il.DeclareLocal(typeof(byte).MakeByRefType(), true);
    // pin the source
    il.Emit(OpCodes.Ldarg_1);
    il.Emit(OpCodes.Ldarg_2);
    il.Emit(OpCodes.Ldelema, typeof(byte));
    il.Emit(OpCodes.Stloc_0);
    // pin …
Run Code Online (Sandbox Code Playgroud)

clr cil reflection.emit

5
推荐指数
1
解决办法
488
查看次数

下一行输入不同的文字

我有用于诊断目的的文本框.背后的代码非常简单:

XAML:

<TextBox HorizontalAlignment="Left" Margin="640,20,0,0" TextWrapping="Wrap" Height="280" Width="840" Name="txtDiagnostic" IsHitTestVisible="True" />
Run Code Online (Sandbox Code Playgroud)

C#:

private void AddMessage(string message)
{
    txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message);
}
Run Code Online (Sandbox Code Playgroud)

如何定义每个新输入位于不同的行?因为现在所有的错误只有一长串.

14:15:00错误1 14:16:00错误2 14:17:00错误3

而不是像每个错误之间的换行符一样可读,如下例所示:

14:15:00错误1 14:16:00错误
2
14:17:00错误3

c# xaml textbox uwp

5
推荐指数
1
解决办法
124
查看次数

从属性返回字符串连接的最有效方法

我已经在这里阅读了一些帖子,常见的建议是如果加入三个字符串,stringbuilder是最有效的.

所有变量都是其他属性.

public string Summary
{
  get 
  {
    return Name.Replace("_", " ") + "<strong>[" + Total + " Devices - " + BadCount + " Offline, " + PendingCount + " Pending]</strong>";
  }
}
Run Code Online (Sandbox Code Playgroud)

我加入四,是一个简单的连接适合或我应该使用stringbuilder?只是看起来有点矫枉过正.

c# string stringbuilder concatenation

4
推荐指数
1
解决办法
963
查看次数

如何在c#中获取字符串中的所有其他字符

所以我正在解决这个问题:https : //www.hackerrank.com/challenges/30-review-loop/problem(它在 C# 中)

到目前为止,我只是试图将它一块一块地分解,到目前为止,我能够让它显示所有其他字符,但我不确定如何将每个字母连接成一个新字符串。

我的问题代码如下我已经注释掉了两个 for 循环,因为我觉得有一个比我拥有的更优雅的解决方案,但我不想丢失我所在的位置以防万一另一条路径事实证明更具挑战性。

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int inputQTY = Int32.Parse(Console.ReadLine());
        string input = Console.ReadLine(); // The example gives us the first word to be Hacker then the next word Rank on the next line, so the outputs would be Hce akr, …
Run Code Online (Sandbox Code Playgroud)

c# string loops

3
推荐指数
1
解决办法
3295
查看次数

连接字符串最有效的方法?

连接字符串以接收ukr:'Ukraine';rus:'Russia';fr:'France'结果的最佳方法是什么?

public class Country
{
    public int IdCountry { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
}

var lst = new List<Country>();
lst.Add(new Country(){IdCountry = 1, Code = "ukr", Title = "Ukraine"});
lst.Add(new Country() { IdCountry = 2, Code = "rus", Title = "Russia" });
lst.Add(new Country() { IdCountry = 3, Code = "fr", Title = "France" });
string tst = ????
Run Code Online (Sandbox Code Playgroud)

c# lambda concatenation

2
推荐指数
1
解决办法
2693
查看次数

将元组列表转换为字符串的更好方法

我在下面的结构中有一个List:

Tuple<string, string>
Run Code Online (Sandbox Code Playgroud)

像这样:

在此输入图像描述

我想加入列表,形成如下字符串:

['A', '1'],['B', '2'], ['C', '3']...
Run Code Online (Sandbox Code Playgroud)

我现在使用下面的代码来做:

string result = "";
for (int i = 0; i < list.Count; i++)
{
      result += "[ '" + list[i].Item1 + "', '" + list[i].Item2 + "'],";
}
Run Code Online (Sandbox Code Playgroud)

代码工作正常,但想问是否有更好的方法可以做到这一点?

c# string

2
推荐指数
2
解决办法
3989
查看次数