简体   繁体   English

c#WPF中的文字字符过多

[英]Too many Literal Characters in c# WPF

String value in my code is like: 我的代码中的字符串值如下:

string abc = "Page1: This is my new Page1 -------- Page2: This is my new Page 2
-------- Page3: This is my new Page3 -------- "

I want to split it like: 我想把它拆分成:

Page1 第1页

This is my new Page1 这是我的新Page1

Page2 第2页

This is my new Page 2 这是我的新篇章

Page3 第3页

This is my new Page3 这是我的新Page3

I wrote this code but it says that There are two many Literals 我写了这段代码,但它说There are two many Literals

 string value =  "Page1: This is my new Page1     --------     Page2: This is my new Page 2     --------     Page3: This is my new Page3     --------     ";


 char[] delimiters = new char[] { '    --------     ' };

 string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

 for (int i = 0; i < parts.Length; i++)
  {
     MesaageBox.Show(parts[i]);
  }

 parts = value.Split(new string[] { "\r\n" }, StringSplitOptions.None);

  for (int i = 0; i < parts.Length; i++)
   {
      MesaageBox.Show(parts[i]);
   }

Your error is that line; 你的错误是那条线;

char[] delimiters = new char[] { '    --------     ' };

Obviusly, -------- is not a single character. 显而易见, --------不是一个单一的角色。 It is a string . 这是一个字符串 Use string array instead of a char array like; 使用string数组而不是char数组;

string[] delimiters = new string[] { "    --------     " };

Full example; 完整的例子;

string value =  "Page1: This is my new Page1     --------     Page2: This is my new Page 2     --------     Page3: This is my new Page3     --------     ";
string[] delimiters = new string[] { "     --------     " };
string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

foreach (var item in parts)
{
    Console.WriteLine(item);
}

Output will be; 输出将是;

Page1: This is my new Page1
Page2: This is my new Page 2
Page3: This is my new Page3

Here a demonstration . 这是一个demonstration

If you want to output like; 如果你想输出像;

Page1
This is my new Page1
Page2
This is my new Page 2
Page3
This is my new Page3 

You just need to add : in your string array to split part. 您只需要在字符串数组中添加:以分割部分。 Like; 喜欢;

string[] delimiters = new string[] { "     --------     ", ":"};

and how to get page1 page 2 ? 以及如何获得第1页第2页?

You can get them like; 你可以得到它们;

Console.WriteLine(delimiters[0]); //Prints Page1
Console.WriteLine(delimiters[2]); //Prints Page2

The issue is that you need to use the string[] overload: 问题是你需要使用string[]重载:

string[] delimiters = new string[] { "    --------     " };

A char is defined as a single character. char被定义为单个字符。 So, when you do this ' -------- ' it can't build that. 所以,当你这样做' -------- '时就无法构建它。 That's not a char , that's a string . 这不是一个char ,这是一个string And quite frankly, that's what you're looking for. 坦率地说,这就是你要找的东西。

So, by changing it from a char[] to a string[] you'll use the right overload. 因此,通过将它从char[]更改为string[]您将使用正确的重载。

Just for the record. 仅供记录。 Here is another option using a regular expression: 这是使用正则表达式的另一个选项:

string value =  "Page1: This is my new Page1     --------     Page2: This is my new Page 2     --------     Page3: This is my new Page3     --------     ";
string[] delimiters = new string[] { "     --------     " };
string[] parts = Regex.Split(value, @"(?<=Page\d+): |\s+-{8}\s+");

foreach (var item in parts)
{
    Console.WriteLine(item);
}

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

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