此类言论该如何处理?
我有一个字符串变量,最多有 80 个字符。 我将变量声明为 CHARACTER STRING*80
或 CHARACTER*80 STRING
如果用户输入的字符少于 80 个,则会在剩余位置附加一些特殊字符。
请问可以告诉我怎么处理吗??
澄清: 我想知道如何用其他字符填充字符串中的剩余空间。
I have String variable which will have maximum 80 characters.
I am declaring a variable as CHARACTER STRING*80
or CHARACTER*80 STRING
If user enters less than 80 characters,then some special characters are getting appended in remaining position.
Could you please let me know how to handle??
CLARIFICATION:
I would like to know how to fill the remaining space in the string with other characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用空字符串预先定义字符串,并用空格填充它。然后使用修剪来仅获取没有尾随空格的字符串。您还应该在声明中使用character(len=80)。
编辑,更新:您可以将字符串中的尾随空格替换为其他字符,例如
Pre-define your string with an empty one, to fill it with spaces. And afterwards use trim to only get the string without trailing spaces. You should also use character(len=80) in the declaration.
Edit, Update: You can replace the trailing spaces in the string with some other character with something like
我不明白“如果用户输入的字符少于 80 个,则一些特殊字符将附加在剩余位置。” -- Fortran 方式是在 IO 或赋值时附加空格来填充字符串。您可以通过以下测试程序看到这一点。它还包括 haraldkl 所示的将尾随空格更改为其他字符的方法。问题是很难区分“真正的”尾随空白和填充空白。
I don't understand "If user enters less than 80 characters,then some special characters are getting appended in remaining position." -- the Fortran way is to append blanks to fill a string on IO or assignment. You can see that with the following test program. It also includes the method shown by haraldkl to change trailing blanks to some other character. A problem is that it is difficult to distinguish between "real" trailing blanks and ones that are padding.
您可以定义一个由您选择的 80 个字符(例如空格)组成的字符串,然后逐个字符获取输入,从而保持所有元素不变:
[' ', ' ', ' ' ... ' ']
['h', ' ', ' ' ... ' ']
['h', 'e', ' ' ... ' ']
['h', 'e', 'l' ... ' ']
You could define a string of 80 characters of your choice (say, spaces) and then get input character by character, therefore leaving all elements untouched:
[' ', ' ', ' ' ... ' ']
['h', ' ', ' ' ... ' ']
['h', 'e', ' ' ... ' ']
['h', 'e', 'l' ... ' ']