C#中的字符串问题 [英] string problem in C#

查看:62
本文介绍了C#中的字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,

this is my code,

string text = System.IO.File.ReadAllText(@"D:\Compiler\test.txt");
            Console.WriteLine(text);



            char[] array = ar.ToCharArray();
            char[] asa;
            for (int i = 0; i <= array.Length - 1; i++)
            {
                        if (array[i] == ' ')
                        {
                            

                        }


                  else
                  {
                   letter =array[i]; 
                   Console.Write(letter);
                  }

                }










此代码文本中的
按字符读取。我希望这样做,如果一个空格将出现,那么单词就完成了,每个单词都在它自己的数组中。例如:

void main void ,,,,,

输出:

exampleArray [0] = void

exampleArray [1] = main

exampleArray [2] = void

如何做到这一点请帮助.............






in this code text read character by character. i want to do this if a space will come then word is complete and each word into it''s own string in an array. for example:
void main void,,,,,
output:
exampleArray[0] = void
exampleArray[1] = main
exampleArray[2] = void
how to do this please help.............

推荐答案

由于你的输入文件建议你正在编写一个编译器(可能是一个类),基于空格拆分输入可能会回来引起麻烦。 空格很重要的情况。例如,在字符串和字符常量中。

从长远来看,你可能会更好地实现一个简单的状态机来将输入文本作为 IEnumerable< char> 并返回表示令牌的对象序列。即,标识符,整数/字符/字符串/浮点数/双常量,各种标点符号(单个和多个字符......)。这是您输入的词法分析。在处理过程中,您可以区分保留字和用户定义的ID。
Since your input file suggested that you are writing a compiler (likely for a class), splitting the input based on spaces may come back to cause trouble. There are cases where spaces are significant. For example, in string and character constants.
In the long run, you would probably do better to implement a simple state machine to take the input text as an IEnumerable<char> and return a sequence of objects representing the "token". I.e., identifier, integer/char/string/float/double const, various punctuation symbols (single and multiple character...). This is the lexical analysis of your input. Further along in the processing you can differentiate between reserved-words and user-defined ids.


有一种更简单的方法:



There is an easier way:

string[] parts = ar.Split(' ');





结果是一个字符串数组,在空格处分割。



如果在(和)这里拆分建议的额外要求:



首先,添加一个最后的gremlin,例如''''',这不应该是你看过的符号之一。在空格分割后你得到



The result is an array of string, split at the spaces.

For the additional requirement if splitting at ( and ) here the proposal:

First, add a "gremlin" at the end, e.g. ''@'', this should not be one of your watched symbols. After split at spaces you get

[0]=void
[1]=main(void)@

在这种情况下你重复在每个字符串上使用first''('',迭代部分将它们添加到新集合并添加字符串(在它们之间。现在你有

In this case you repeat on every string and split using first ''('', iterate over the parts adding them to a new collection and adding string "(" between them. Now you have

[0]=void
[1]=main
[2]=(
[3]=void)@

之后你迭代在'')''分割的所有部分,将它们添加到一个新的集合,并在它们之间添加字符串)。

现在你有

After that you iterate over all parts splitting at '')'', adding them to a new collection and adding string ")" between them.
Now you have

[0]=void
[1]=main
[2]=(
[3]=void
[4]=)
[5]=@

完成后删除''gremlin'',或者从最后一个字符串的末尾开始,如果它有超过2个字符,或者是整个最后一个字符串。

然后你得到:

After you are done remove the ''gremlin'', either from the end of the last string if it has more than 2 characters, or the whole last string.
Then you get:

[0]=void
[1]=main
[2]=(
[3]=void
[4]=)



我想你得到的照片。


I think you get the picture.


你好Arsalan,



怎么样这是
Hello Arsalan,

How about doing this as
string[] words = text.Split(' ');



问候,


Regards,


这篇关于C#中的字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆