【问题标题】:Concatenation specified sequence连接指定序列
【发布时间】:2021-11-08 10:23:25
【问题描述】:

我有任务,执行下一步 - 将六个字符从源(字符串)复制到目标(字符串)。第一个字符串看起来像 - “example_da”,第二个 - “this_is_*******_”,因此任务由副本“source”(仅“example”)组成,并将其插入到间隔 *******。但是当尝试使用 source.ToCopy 我得到 - 索引和计数必须引用字符串中的位置(但如果长度为 15,为什么我不能这样做)

嵌套代码:

 public static string CopySixChars(string source, string destination)
    {

        char[] destinationArray = destination.ToCharArray();

        source.CopyTo(4, destinationArray, 0, source.Length);

        return new string(destinationArray);
    }

【问题讨论】:

  • 忘记添加主要问题)
  • 此时您可能会感到沮丧,值得您学习一些简单的字符串教程。我可以给你答案dest = source.Substring(0,6);,但你需要知道如何做所有这些类型的事情(不必在 Stackovereflow 上询问)所以我建议更多地通过动手练习来学习......
  • @JeremyThompson ,是的,很可能有必要阅读文档,尽管我之前的连接方法代码有 Substring(我 2 小时前刚上床睡觉)。我最近使用它,对不起这个愚蠢的问题,谢谢!

标签: c# string visual-studio concatenation


【解决方案1】:

也许你可以参考一下。

语法:

public void CopyTo (
int source_index, //index to the string from where you want to copy the string
char[] destination, //target character array in which you want to copy the part of the string
int destination_index, //index in the targeted character array
int count//total number of characters to be copied in character array
);

主要:

string a = "example_da";
string b = "this_is_*******_";
Console.WriteLine( CopySixChars(a, b));

嵌套代码:

public static string CopySixChars(string source, string destination) {
            char[] destinationArray = destination.ToCharArray();
            source.CopyTo(0,destinationArray,8,6);
            return new string(destinationArray);
        }

打印:

【讨论】:

  • 对你有帮助吗?请给我回复更好的帮助你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2022-07-07
  • 2018-09-08
相关资源
最近更新 更多