首页 \ 问答 \ C ++从字符串中去除非ASCII字符(C++ Strip non-ASCII Characters from string)

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字符? (在C#中)

如何去除c ++中的字符串中的所有非字母数字字符?

编辑:

我仍然想删除所有非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 for isalnum

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
更新时间:2022-03-01 20:03

最满意答案

您可以自动为站点,角色和用户组分配新用户,但不能为组织分配新用户。 无论如何,只需几行代码,肯定有很多方法可以达到你想要的效果。

例如,您可以开发一个钩子插件,它覆盖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 the UserLocalService service, so you can add a new organization ID to the organizationIds parameter before calling super.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 ...
  • 您正在以最佳方式(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 ...
  • 您将获得“自助服务” - 例如申请会员资格 - 使用常规网站。 让它们受到保护,你将得到你正在寻找的东西。 按照设计,组织没有这种能力。 考虑到这一点,网站意味着支持自愿成员资格,而组织(你可以通过名称来思考)“让你成为他们的成员”,反之亦然。 因此,如果您希望为组织提供此自助服务功能,则必须(完全)自己实现此功能。 You'll get the "self service" - e.g. applying for membership - with regular Sites. make them pro ...
  • 这是我一直在做的,这是一个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
  • 我会有以下表格: 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)