Ada - 修剪和删除字符串上的特殊字符
我有一个名为输入的变量,
Input : STRING (1 .. 80) ;
我使用 Ada.Strings 和 Ada.Text_IO 作为库,我需要知道是否可以以及如何删除输入变量上的空格和特殊字符(例如顶点、逗号和句点)。
I have a variable called Input
Input : STRING (1 .. 80) ;
I use Ada.Strings and Ada.Text_IO as library and I need to know if I can, and how, to remove spaces and special characters (such as the apex, the comma and period) on Input Variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有
Ada.Strings
,则很可能有Ada.Strings.Fixed
和Ada.Strings.Maps
。在
Ada.Strings.Maps
的帮助下,使用Ada.Strings.Fixed.Translate
方法将任何字符转换为任何其他字符。或者,您可以只循环输入字符串,然后仅将所需的字符附加到新字符串中。 (不太优雅,但你不需要任何其他库)
If you have
Ada.Strings
, you most likely haveAda.Strings.Fixed
andAda.Strings.Maps
.Use the
Ada.Strings.Fixed.Translate
method(s) with assistance fromAda.Strings.Maps
to translate any characters to any other characters.Alternatively you can just loop over your input string and only append characters that you want to a new string. (less elegant, but you dont need any other libraries)
Ada.Strings.Maps 包在这里可能会非常有帮助,特别是“To_Set”(定义要在Character_Sequence 中删除的字符)和“Is_In”函数。
由于您要删除而不是替换字符,因此您必须遍历字符串,检查每个字符是否“位于”要删除的字符集中。如果是这样,请勿将其附加到输出字符串缓冲区。
The Ada.Strings.Maps packages would likely be very helpful here, particularly the "To_Set" (define the characters you want to strip out in a Character_Sequence) and "Is_In" functions.
Since you're deleting, rather than replacing, characters, you'll have to iterate through the string, checking to see whether each one "is in" the set of those to be deleted. If so, don't append it to the output string buffer.
好吧,首先您必须了解,如果您想从字符串中间删除一个字符,则删除字符之后的所有字符都必须移至左侧一个字符。这不是 Ada 特有的问题。如果字符串很大,这可能会相当低效。不过,我不会将 80 个或更少的字符称为“大”。
您必须了解的第二件事是,与许多语言不同,在 Ada 中,字符串的大小应该是完美的。这意味着,如果您要使用 String 变量作为缓冲区(例如:通过 Text_IO 输入),您还必须跟踪其中有效数据的实际长度另一个变量。
鉴于此,编写代码来识别您不想要的字符,并使用以下内容删除它们可能是一个简单的问题:
但是,这是一种困难的方法。如果您确实想要以这种方式操作一个字符串,那么将其放入无界字符串对象中可能是最简单的方法。
Ada.Strings.Unbounded.Find_Token
和Ada.Strings.Unbounded.Delete
将满足您的需要,您只需编写一个围绕它们的循环即可。Well, first off you have to understand that if you want to remove a character potentially from the middle of a string, all the characters after the removed character will have to be shifted over to the left one character. This isn't an Ada-specfic issue. If the string is big, this could be rather inefficient. I wouldn't call 80 characters or less "big" though.
The second thing you have to understand is that in Ada, unlike many languages, strings are meant to be perfectly-sized. That means that if you are going to use a
String
variable as a buffer (eg: for input via Text_IO), you are going to have to also keep track of the actual length of valid data in it with another variable.Given that, it can be a simple matter of writing code to recognize the characters you don't want in there, and remove them with something like:
However, this is kind of the hard way. If you really have a string you want to manipulate this way, it is probably easiest to put it in an unbounded string object.
Ada.Strings.Unbounded.Find_Token
andAda.Strings.Unbounded.Delete
will do what you need, and all you have to write is a loop around them.