C ++从字符串中去除非ASCII字符(C++ Strip non-ASCII Characters from string)
在你开始之前; 是的,我知道这是一个重复的问题,是的,我已经看过发布的解决方案。 我的问题是我无法让他们工作。
bool invalidChar (char c) { return !isprint((unsigned)c); } void stripUnicode(string & str) { str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end()); }
我在“Prusæus,Ægyptians”上测试了这个方法,它也没有做任何事情,我也试图用
isalnum
替代isprint
真正的问题发生在我的程序的另一部分中时,我将string-> wstring-> string转换。 如果string-> wstring转换中存在unicode字符,则转换会变得很慢。
参考:
编辑:
我仍然想删除所有非ASCII字符,无论它是否有帮助,这里是我崩溃的地方:
// Convert to wstring wchar_t* UnicodeTextBuffer = new wchar_t[ANSIWord.length()+1]; wmemset(UnicodeTextBuffer, 0, ANSIWord.length()+1); mbstowcs(UnicodeTextBuffer, ANSIWord.c_str(), ANSIWord.length()); wWord = UnicodeTextBuffer; //CRASH
错误对话框
MSVC ++调试库
调试断言失败!
程序:// myproject
文件:f:\ dd \ vctools \ crt_bld \ self_x86 \ crt \ src \ isctype.c
行://上方
表达:(无符号)(C + 1)<= 256
编辑:
进一步加剧了这个问题:我正在读取的.txt文件是ANSI编码的。 所有内容都应该是有效的。
解:
bool invalidChar (char c) { return !(c>=0 && c <128); } void stripUnicode(string & str) { str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end()); }
如果有人想复制/粘贴,我可以检查这个问题。
编辑:
以供将来参考:尝试使用__isascii,iswascii命令
Before you get started; yes I know this is a duplicate question and yes I have looked at the posted solutions. My problem is I could not get them to work.
bool invalidChar (char c) { return !isprint((unsigned)c); } void stripUnicode(string & str) { str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end()); }
I tested this method on "Prusæus, Ægyptians," and it did nothing I also attempted to substitute
isprint
forisalnum
The real problem occurs when, in another section of my program I convert string->wstring->string. the conversion balks if there are unicode chars in the string->wstring conversion.
Ref:
How can you strip non-ASCII characters from a string? (in C#)
How to strip all non alphanumeric characters from a string in c++?
Edit:
I still would like to remove all non-ASCII chars regardless yet if it helps, here is where I am crashing:
// Convert to wstring wchar_t* UnicodeTextBuffer = new wchar_t[ANSIWord.length()+1]; wmemset(UnicodeTextBuffer, 0, ANSIWord.length()+1); mbstowcs(UnicodeTextBuffer, ANSIWord.c_str(), ANSIWord.length()); wWord = UnicodeTextBuffer; //CRASH
Error Dialog
MSVC++ Debug Library
Debug Assertion Failed!
Program: //myproject
File: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c
Line: //Above
Expression:(unsigned)(c+1)<=256
Edit:
Further compounding the matter: the .txt file I am reading in from is ANSI encoded. Everything within should be valid.
Solution:
bool invalidChar (char c) { return !(c>=0 && c <128); } void stripUnicode(string & str) { str.erase(remove_if(str.begin(),str.end(), invalidChar), str.end()); }
If someone else would like to copy/paste this, I can check this question off.
EDIT:
For future reference: try using the __isascii, iswascii commands
原文:https://stackoverflow.com/questions/10178700
最满意答案
您可以自动为站点,角色和用户组分配新用户,但不能为组织分配新用户。 无论如何,只需几行代码,肯定有很多方法可以达到你想要的效果。
例如,您可以开发一个钩子插件,它覆盖
UserLocalService
服务的addUserWithWorkflow
方法,因此您可以在调用super.addUserWithWorkflow(...)
之前向organizationIds
参数添加新的组织ID。希望能帮助到你!
You can automatically assign new users to sites, roles and user groups, but not organizations. Anyway, there are surely many ways to achieve what you want with just a few lines of code.
For example, you can develop a hook plugin which overrides the
addUserWithWorkflow
method of theUserLocalService
service, so you can add a new organization ID to theorganizationIds
parameter before callingsuper.addUserWithWorkflow(...)
.Hope it helps!
相关问答
更多-
没有任何开箱即用的功能。 我无法在事件API中看到任何可插入的内容。 There's nothing out of the box that does this. I can't see anything in the events API that you could plug into either.
-
1)站点/组织管理员无法删除任何其他管理员或所有者的成员身份。 2)站点/组织管理员无法删除其他管理员/所有者的站点/组织管理员或所有者角色。 3)站点/组织所有者可以删除成员资格或从任何其他管理员或所有者中删除角色。 从这个链接 1) A Site/Organization Administrator cannot delete the membership of any other Administrator or Owner. 2) A Site/Organization Administrator ...
-
不幸的是,它没有公共API。 使用LayoutSetPrototypeLocalServiceUtil获取SiteTemplate的ID。 要按名称获取SiteTemplate,您必须使用dynamicQuery或迭代LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypes(-1,-1)的结果 然后在门户网站的上下文中调用SitesUtil的applyLayoutSetPrototypes。 MethodKey methodKey = new Met ...
-
Gitlab:是否可以自动将新用户分配给项目组(Gitlab: Is it possible to auto assign new users to project groups)[2022-02-09]
您正在以最佳方式(API)为您的身份验证方法执行此操作。 您可以使用内置的组同步在GitLab Enterprise Edition中通过LDAP / AD集成轻松完成此操作。 我们一直在寻求改进GitLab。 什么会让GitLab在这方面更方便? 请随时在feedback.gitlab.com上提交您的想法。 You're doing it in the best way possible (API) for your authentication method. You could do this ea ... -
Liferay,用户加入组织(Liferay ,user join Organization)[2023-10-14]
您将获得“自助服务” - 例如申请会员资格 - 使用常规网站。 让它们受到保护,你将得到你正在寻找的东西。 按照设计,组织没有这种能力。 考虑到这一点,网站意味着支持自愿成员资格,而组织(你可以通过名称来思考)“让你成为他们的成员”,反之亦然。 因此,如果您希望为组织提供此自助服务功能,则必须(完全)自己实现此功能。 You'll get the "self service" - e.g. applying for membership - with regular Sites. make them pro ... -
将用户分配到组modx(assign users to group modx)[2022-05-23]
这是我一直在做的,这是一个posthook片段,在用户注册[并创建用户]后触发 getValue('specialty'); $country = strtolower($hook->getValue('excountry')); $username = $hook->getValue('username'); $staff = $hook->getValue('staff-or-resident'); //Staff | Resident $join ... -
您必须通过$ user.getOrganizations()获得用户组织。 并使用foreach迭代它 You have to get user organizations by $user.getOrganizations(). And iterate over it using foreach
-
DB Schema Organization(DB Schema Organization)[2022-06-17]
我会有以下表格: User (UserID, firstname, lastname, etc.) Event (EventID, Name, Date, Location, Capacity, etc.) EventRegistration (EventRegistrationID, UserID, EventID, ParticipantTypeID, etc.) ParticipantType (ParticipantTypeID, Name) ParticipantType.Name是“参与者”或 ... -
您可以自动为站点,角色和用户组分配新用户,但不能为组织分配新用户。 无论如何,只需几行代码,肯定有很多方法可以达到你想要的效果。 例如,您可以开发一个钩子插件,它覆盖UserLocalService服务的addUserWithWorkflow方法,因此您可以在调用super.addUserWithWorkflow(...)之前向organizationIds参数添加新的组织ID。 希望能帮助到你! You can automatically assign new users to sites, roles ...
-
这个工作正常,但是当使用auto_register功能时,它会忽略我的authlogic选项,例如从openid提供程序检索电子邮件......任何想法我做错了什么? 处理自动注册的代码存在于authlogic_openid的Session模块中。 处理注册(检索电子邮件表单提供程序等)的代码存在于ActsAsAuthentic模块中。 第一个是处理UserSession对象,后者正在处理User对象。 我将在几天后看看,看看可以做些什么来合并这两种行为。 This one works fine howev ...