如何在.aspx中删除特殊字符 [英] How do I strip special chars in .aspx

查看:117
本文介绍了如何在.aspx中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,什么是最简单的方式



Eval(some_phone)。ToString()



输出7078884524而不是(707)888 4524?



我尝试过:



我试过谷歌搜索类似的问题,但没有发现任何适用

解决方案

这样的事情:

 受保护 静态 字符串 AlphaNumericOnly( string   value 
{
if string .IsNullOrEmpty( value ))返回 string .Empty;

char [] result = new char [ value .Length];
int resultIndex = 0 ;

foreach char c in value
{
if ' 0' < = c&& c < = ' 9'
{
result [resultIndex] = c;
resultIndex ++;
}
else if ' a' < = c&& c < = ' z'
{
结果[resultIndex] = c;
resultIndex ++;
}
else if ' A' < = c&& c < = ' Z'
{
结果[resultIndex] = c;
resultIndex ++;
}
}

if (resultIndex == 0 return string .Empty;
if (resultIndex == value .Length) return value ;
return new string (结果, 0 ,resultIndex);
}



< asp:Literal runat =servermode =Encode
Text ='< %#AlphaNumericOnly(Eval(Field,{0}))%>'
/>


引用:

如何在.aspx中删除特殊字符

基本上,你有2个解决方案:

- 建立你自己的一段代码。

扫描(707)888 4524中的每个字符。
当一个数字附加到结果时。





- 使用RegEx匹配不必要的字符,然后将其替换为删除它们。



两者解决方案对于初学者来说很复杂,并且是很好的练习练习。



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建Re的工具的链接gEx并调试它们:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


尝试 String.Replace方法(字符串,字符串) (系统) [ ^ ]

 String phone =(707)888 4524; 
Console.WriteLine(电话);
phone = phone.Replace(),);
phone = phone.Replace((,);
phone = phone.Replace(,);
Console.WriteLine(phone);


For example, what's the easiest way for

Eval("some_phone").ToString()

to output 7078884524 instead if (707) 888 4524 ?

What I have tried:

I tried googling similar questions but found nothing that applies

解决方案

Something like this:

protected static string AlphaNumericOnly(string value)
{
    if (string.IsNullOrEmpty(value)) return string.Empty;
    
    char[] result = new char[value.Length];
    int resultIndex = 0;
    
    foreach (char c in value)
    {
        if ('0' <= c && c <= '9')
        {
            result[resultIndex] = c;
            resultIndex++;
        }
        else if ('a' <= c && c <= 'z')
        {
            result[resultIndex] = c;
            resultIndex++;
        }
        else if ('A' <= c && c <= 'Z')
        {
            result[resultIndex] = c;
            resultIndex++;
        }
    }
    
    if (resultIndex == 0) return string.Empty;
    if (resultIndex == value.Length) return value;
    return new string(result, 0, resultIndex);
}


<asp:Literal runat="server" mode="Encode"
    Text='<%# AlphaNumericOnly(Eval("Field", "{0}")) %>'
/>


Quote:

How do I strip special chars in .aspx

Basically, you have 2 solutions:
- Build your own piece of code.

Scan every char in "(707) 888 4524"
   when a digit, append to result.



- Use the RegEx to match unnecessary chars and replace then by nothing to remove them.

Both solutions are complicated only for very beginners and are good exercises for practicing.

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


Try String.Replace Method (String, String) (System)[^]

String phone = "(707) 888 4524";
Console.WriteLine(phone);
phone = phone.Replace(")", "");
phone = phone.Replace("(", "");
phone = phone.Replace(" ", "");
Console.WriteLine(phone);


这篇关于如何在.aspx中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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