使用C#进行字符串操作 [英] String operation using c#

查看:81
本文介绍了使用C#进行字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想从以下字符串中删除 password =''efgh''.如何使用c#做到这一点.

 <?  xml    版本  ="  ? >  <   wql    主机  192.168.1.127' 用户名  ='  abcd'   密码  ='  efgh'   类型  ='  XYZ' > 
<  查询    ='    ='   设备类型  ='  123' >  <![CDATA [  从abc中选择*  ]]>  
<  /query  >  <  /wql  >  



问候
Sebastian

解决方案

您可以使用RegEx(正则表达式).我不是RegEx专家,但是有关信息,请访问 http://www.knowdotnet.com/articles/regereplacementstrings.html [ ^ ].您需要的正则表达式为"password=\''[a-z]+\''".这是一些代码,表明它可以工作:

 字符串 x = " ;
 Regex rx =  Regex(" );
 var  r = rx.Match(x);
如果(r.Groups.Count >   0 )
{
     var  g1 = r.Groups [ 0 ]; //  = password ='edere'
     var  g2 = r.Groups [ 1 ]; //  = edere 
} 



请注意,我在"[a-z]+"周围放置了(),这使您可以获取实际的密码.


尝试一下:

  string  output = xmlstring.Replace(" " );  


Hello,

I want to remove password=''efgh'' from the following string. How can I do that using c#.

<?xml version="1.0"?><wql host='192.168.1.127' username='abcd' password='efgh' Type='XYZ'>
<query id='0.' ns='root\cimv2' devicetype='123'><![CDATA[select * from abc]]>
</query></wql>



Regards
Sebastian

解决方案

You can use RegEx (regular expressions). I am not an expert with RegEx, but there is information at http://www.knowdotnet.com/articles/regereplacementstrings.html[^]. The regular expression you would need is "password=\''[a-z]+\''". Here is some code to show that this works:

string x = "sdlkfsdal password='edere' dfldskflkds";
 Regex rx = new Regex("password=\'([a-z]+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
    var g1 = r.Groups[0]; // = password='edere'
    var g2 = r.Groups[1]; // = edere
}



Notice I put () around the "[a-z]+", which allows you to get the actual password.


Try this :

string output = xmlstring.Replace("password='efgh'","");


这篇关于使用C#进行字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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