如何剪切文本中所有不需要的字符? [英] How to cut all unwanted characters in a text?

查看:66
本文介绍了如何剪切文本中所有不需要的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在从电子邮件的内容主体中解析电子邮件ID.同时解析具有richa@carteworld.com作为电子邮件ID的内容时,将其视为3Aricha@carteworld.com.我仔细研究了一下问题,发现在内容正文中找不到一个看不见的字符(即,内容正文中可能有字符,但可能不是电子邮件ID的一部分)但它会出现在内容主体中),因此它会像这样拾取.有谁能帮我削减内容文本正文中所有不​​需要的特征.

渴望等待最早的答复


问候
Kumaran

Hi everybody,

I am parsing email Ids from body of content of the email. while parsing a body of content having richa@carteworld.com as email Id it is taken 3Aricha@carteworld.com. I dig through the problem and came to know that there is a invisible character which i can''t able to find in the body of the content (i.e. there may be character in the body of content which may not be part of the email id but it will in body of content) so it picks up like this. can any one help me out to cut all unwanted charactes in the body of content text.

eager waiting for earliest replies


regard
Kumaran

推荐答案

如果您知道字符是什么,可以尝试这样的事情

If you know what the character(s) is(are) you can try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char EvilHiddenChar = (char)3;
            string EmailMessage = string.Format("Hello{0}, this {0}is my {0}E-mail message", EvilHiddenChar);
            Console.WriteLine(EmailMessage);
            EmailMessage = EmailMessage.Replace(EvilHiddenChar.ToString(), String.Empty);
            Console.WriteLine(EmailMessage);
            Console.ReadLine();
        }
    }
}



希望对您有所帮助:cool:



Hope this helps :cool:


您可以采用多种方法.如果知道要切出的字符,则可以使用string.Split删除字符,然后使用string.Concat重新组合刚刚分割的字符串.或者,如果仅知道要保留的字符(az,0-9,标点符号等),则可以使用正则表达式查找与这些字符不匹配的所有字符,然后可以调用Regex.Replace删除所有匹配项.那会是这样的:
There are various approaches you could take. If you know the characters you want to cut out, you can use string.Split to remove the characters and then string.Concat to recombine the string that you just split. Alternatively, if you only know the characters you want to keep (a-z, 0-9, punctuation, and so on), then you can use a regular expression to find all characters that do not match those characters and you could call Regex.Replace to remove any matches. That would go something like this:
string input = "This is some string where I only want letters and numbers.";
string pattern = "((?![a-z]|[0-9]).)+";
string result = Regex.Replace(input, pattern, string.Empty);


我将留给您构造所需的确切正则表达式.


I will leave it to you to construct the exact regular expression you want.


这篇关于如何剪切文本中所有不需要的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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