将字符串转换为指定规则的子字符串 [英] Convert a string to a substring of the specified rules

查看:104
本文介绍了将字符串转换为指定规则的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文本文件中读取了3行。然后我将它们转换成几个子串

我该怎么做。

应该

  ' ' 

忽略它。

输入:

  int  s; 
int f;
d = 12 ;
c = 10 ;



输出:

 string a = { int ,s,;}; 
string b = { int ,f ,;};
string c = {d,=, 12 ,;};
string f = {c,=, 10 ,;};

解决方案


这很简单。首先,每行应该用Enter键分隔。然后解析行直到Keys.Enter并将你的字符串添加到行。

Yours Farhad 。


如果您的解析器不必太一般(即示例中有它必须处理的所有内容)并且我不喜欢使用解析器生成器我会做类似的事情:



 while(lines_to_read)
读取一行
删除终止分号

这行是否有'='?
是:这是一个赋值
分割成两行的'='字符
否:这是一个声明
在第一个空白字符分割成两行字符

修剪每个子字符串的空格
表格输出字符串


你必须写一个(非常简单的)词法分析器。 Google是您的朋友 [ ^ ]。

I read 3 lines from a text file. Then I would convert them into several sub-string
What do I do.
Should

' '

ignore it.
input:

int  s ;
int f;
d  = 12  ;
c=10;


output:

string a={int,s,;};
string b={int,f,;};
string c={d,=,12,;};
string f={c,=,10,;};

解决方案

Hi It is very simple.First of all each line should seprate with Enter key.Then parse lines until Keys.Enter and add your string to line.
Yours Farhad.


If your parser doesn't have to be too general (i.e. the examples there are all the things it has to cope with) and I didn't fancy messing about with a parser generator I'd do something like:

while( lines_to_read )
    read a line
    remove the terminating semi-colon

    has the line got an '=' in it?
        yes: it's an assignment
            split line into two at the '=' character
        no: it's a declaration
            split line into two at the first white space character

    trim whitespace from each substring
    form output string


You have to write a (very simple) lexer. Google is your friend[^].


这篇关于将字符串转换为指定规则的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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