转换tab为空格在.NET字符串空格、字符串、tab、NET

2023-09-04 09:35:44 作者:木槿暖夏

我建立使用常规的前pressions文本分析器。我需要把所有标签字符串中的字符为空格字符。我不能假设一个标签应该有多少空间涵盖否则我可以替换,比方说,4空格字符的标签。是否有这类问题什么好的解决办法。我需要做的code,所以我不能使用外部工具。

I am building a text parser using regular expressions. I need to convert all tab characters in a string to space characters. I cannot assume how many spaces a tab should encompass otherwise I could replace a tab with, say, 4 space characters. Is there any good solution for this type of problem. I need to do this in code so I cannot use an external tool.

不幸的是,没有这些答案的解决与我遇到的问题。我提取外部文本文件中的文本,我不能假设他们是如何被创造或操作系统来创建它们。我相信制表符的长度可以变化,所以如果我遇到的时候我读文本文件中的标签,我想知道有多少空格字符,我应该把它替换。

Unfortunately, none of these answers address the problem with which I am encountered. I am extracting text from external text files and I cannot assume how they were created or which operating system was used to create them. I believe the length of the tab character can vary so if I encounter a tab when I am reading the text file, I want to know how many space characters I should replace it with.

推荐答案

不幸的是,您需要承担多少空格选项卡重新presents。你应该设置为一个固定值(如这4种),或者使其用户选择。

Unfortunately, you need to assume how many spaces a tab represents. You should set this to a fixed value (like the mentioned four) or make it a user option.

要做到这一点是.NET的最快方法是(我使用C#):

The quickest way to do this is .NET is (I'm using C#):

var NewString = "This is a string with a    Tab";
var TabLength = 4;
var TabSpace = new String(' ', TabLength);

NewString = NewString.Replace("\t", TabSpace);

您可以再TabLength变量设置为你想要的任何东西,通常提到的previously四个空格字符。

You can then change the TabLength variable to anything you want, typically as mentioned previously, four space characters.

在所有操作系统标签具有相同的长度,一个凸片!所不同的是软件显示它们的方式,典型地这是四个空白字符相当于宽度,而这还假定显示器使用固定宽度的字体,例如宋体。

Tabs in all operating systems are the same length, one tab! What differs is the way software displays them, typically this is the equivalent width of four space characters, and this also assumes that the display is using a fixed width font such as Courier New.

例如,我的选择的IDE可以让我的制表符的宽度更改为一个值,适合我。

For example, my IDE of choice allows me to change the width of the tab character to a value that suits me.