将子字符串替换为字符串中的另一个子字符串 [英] Replace a Substring with Another Substring in a String

查看:114
本文介绍了将子字符串替换为字符串中的另一个子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的字符串



I have a string like

output="'TransactionSourceID !='8' and GeneralLedger_SubTransactionType_CnfgLocale.SubTransactionType != '1'";





我需要在 TransactionSourceID 之前删除单引号



我该怎么办?



我试过





I need to remove the single quote before TransactionSourceID alone.

How do I do it ?

I tried

if(output.Contains("'TransactionSourceID"))
                            output.Replace("\'TransactionSourceID","TransactionSourceID");











and

if(output.Contains("'TransactionSourceID"))
                            output.Replace("'TransactionSourceID","TransactionSourceID");





但它不起作用。



我无法使用字符的索引,因为字符串的长度和TransactionSourceID的位置可能会有所不同。



任何解决方案??



But it didn't work.

I cant use index of the character because the length of the string and the position of "TransactionSourceID" may vary.

Any solutions ??

推荐答案

Quote:

if(output.Contains('TransactionSourceID))

output.Replace(\'TransactionSourceID,TransactionSourceID);

if(output.Contains("'TransactionSourceID"))
output.Replace("\'TransactionSourceID","TransactionSourceID");





应该是:



That should be:

if(output.Contains("'TransactionSourceID"))
  output = output.Replace("\'TransactionSourceID","TransactionSourceID");



或简单地说:


or simply:

output = output.Replace("\'TransactionSourceID","TransactionSourceID");


如果你的意思是无法正常工作,你的改变后的字符串不能通过输出变量获得,那么只是尝试分配它:
If you mean by "didn't work" that your changed string wasn't available via the output variable, then just try to assign it:
output = output.Replace(YourCode);





[ String.Replace ]不会修改当前实例的值。相反,它返回一个新字符串,其中所有出现的oldValue都被newValue替换。(来自链接的MSDN页面)。



"[String.Replace] does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldValue are replaced by newValue." (from the linked MSDN page).


这篇关于将子字符串替换为字符串中的另一个子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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