将一个字符串插入另一个字符串所需的建议 [英] Advice required to insert one string into another

查看:97
本文介绍了将一个字符串插入另一个字符串所需的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

介绍及相关信息:



我有一个编辑控件只需要接受带符号的十进制数字(类似 -12.35 )。



我决定通过子类化来实现这个。



WM_CHAR handler似乎运行良好,我需要处理其他几条消息,以完全保护用户不输入无效文本。一个这样的消息是 WM_PASTE



到目前为止,我能够正确地从<$ c获取文本$ c>剪贴画并根据复制的字符串是否确实是十进制数来丢弃或传递消息。



编辑控件的输入有限12个字符。这是通过 EM_SETLIMITTEXT 消息完成的。



我使用纯Winapi C ++ 。不允许使用 boost 等库。



问题:



到目前为止,我测试编辑控件为空的情况,如果内容无效,我会丢弃粘贴。但是,用户可以在编辑控件中选择文本的一部分,然后执行粘贴。像这样的东西(红色字符代表选择):



编辑控制中的文字:123 456 78.9



剪贴板文字:-1A



结果字符串,如果我允许粘贴,则为123-1A78。 9这是格式无效。



这是我需要帮助的部分:



我正确解析识别有效的格式,但我不知道如何将两个字符串正确合并为一个所以我可以测试它。



问题:



1.如何通过粘贴正确形成结果字符串,以便测试其有效性?



2.是否有可以为我进行有效性测试的功能(它应该是区域设置!)?



注意:我不介意自己解析文本,但如果不需要它,为什么要重新发明轮子呢?



我的努力解决这个问题:



通过Internet浏览我发现许多连接/切断/ etc字符串的 std :: 函数,但它们似乎都不能满足我对形成结果字符串的需求。我发现最接近的是 string :: insert(...)但我不知道如何使用它。



至于第二个问题,我通过浏览 C ++参考找到了 std :: strtod ,似乎它可以用来检查字符串的有效性。它似乎也是语言环境,但我不确定它是否是我所寻求的。



为了使这篇文章尽可能简短,我提供了链接到我的这个问题 [ ^ ]有相关代码片段。



感谢您的时间和帮助。



如果需要进一步的信息,我会更新帖子。



祝你好运。

INTRODUCTION AND RELEVANT INFORMATION:

I have an edit control that needs to accept only signed decimal numbers ( something like -12.35 ).

I have decided to implement this via subclassing.

The WM_CHAR handler seems to works well, and I need to handle several other messages to completely protect user from entering invalid text. One such message is WM_PASTE.

So far I was able to properly get the text from the clipboard and to discard or pass the message depending if the copied string is indeed decimal number.

Edit control has limited input to 12 characters. This is done via EM_SETLIMITTEXT message.

I use pure Winapi and C++. No libraries like boost etc are allowed.

PROBLEM:

So far I test the case where edit control is empty and I discard pasting if the content is invalid. However, user can select a portion of the text in the edit control and then perform pasting. Something like this ( red characters represent selection ):

Text in edit control: 12345678.9

Clipboard text : -1A

The resulting string, if I would allow pasting, would be 123-1A78.9 which is invalid format.

This is the part I need help with:

My parsing properly recognizes valid format but I do not know how to properly merge two strings into one so I could test it.

QUESTIONS:

1. How can I properly form the resulting string from pasting so I can test its validity ?

2. Is there a function that can do the validity test for me ( it should be locale aware! ) ?

Note: I do not mind parsing the text myself but why "reinventing the wheel" if there is no need for it?

MY EFFORTS TO SOLVE THIS:

Browsing through Internet I found many std:: functions that concatenate/cut off/ etc string but none of them seem to satisfy my needs for forming the resulting string. The closest thing I found was string:: insert(...) but I do not know how to use it.

As for the second question, I found std:: strtod by browsing through C++ reference and it seems that it can be used to check the validity of the string. And it seems that it is locale aware as well, but I am not sure if it is what I seek.

To keep this post as brief as possible I provide the link to this question of mine[^] that has relevant code snippets.

Thank you for your time and help.

If further info is required I will update the post.

Best regards.

推荐答案

你的第一个问题的答案是:建立一个通过连接新的字符串

a)选择前的部分

b)剪贴板文本

c)选择后的部分。



所以它就像两个连接一样简单。



s的答案第二个问题是:是的,您可以使用strtod进行有效性检查。但请考虑strtod接受多种格式,例如12.4e-13。可能您不希望允许用户输入指数。在这种情况下,你必须编写自己的验证函数,这不是太难。
The answer to your first question is: Just build a new string by concattenating
a) the part before the selection
b) the clipboard text
c) the part after the selection.

So it is as simple as two concattenations.

The answer to the second question is: Yes, you could use strtod for the validity check. But consider that strtod accepts a wide range of formats, e.g. "12.4e-13". Possibly you don't want to allow the user to enter exponents. In that case you will have to write your own validation function, which is not too hard.


这篇关于将一个字符串插入另一个字符串所需的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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