繁体   English   中英

如何将C#字符串一分为二?

[英]How can I divide a C# string into two?

我想输出一个路径:

string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder
Debug.WriteLine((libraryPath));

但是路很长。

如何以两行输出路径。 在第二行的前1/2,然后第二1/2?

使用SubString拆分输出。

// Print 1st half (from index 0 to half of length)
Debug.WriteLine((libraryPath.SubString(0, libraryPath.Length / 2)));
// Print 2nd half (from middle of string to end)
Debug.WriteLine((libraryPath.SubString(libraryPath.Length / 2)));

注意:如果“ Length为奇数,则前半部分将较短。

  1. 查找“库路径”的长度
  2. 使用子字符串将“库路径”分成两半
  3. 将两个半都写入变量
  4. 将每个变量写在单独的行上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM