希望在两个'/'正斜杠之间替换文本 [英] Looking to replace text between two '/' forward slashes

查看:90
本文介绍了希望在两个'/'正斜杠之间替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的数据列中的前两个正斜杠/之间替换文本。



我有以下内容可以让我以前得到一切。



选择ReportLocation,substring(ReportLocation) ,0,Len(ReportLocation)-charindex('/',reverse(ReportLocation))+2)来自Report



然后我尝试了这个改变了所有数据相同的列。



声明@Url varchar(最大值)



I trying to replace text between the first two forward slash "/" in my datacolumn.

I have the following which allows me to get everything before.

Select ReportLocation, substring(ReportLocation, 0, Len(ReportLocation)-charindex ('/', reverse(ReportLocation)) +2) from Report

Then I tried this which changes all the data in the column to the same.

Declare @Url varchar(max)

SELECT   ReportLocation
        ,LEFT(ReportLocation, CHARINDEX('/', ReportLocation) - 0) AS [Surname]
        ,REPLACE(SUBSTRING(ReportLocation, CHARINDEX('/', ReportLocation), LEN(ReportLocation)), '/', 'newone') AS ReportLocation
FROM    Report
where CHARINDEX('/', ReportLocation) > 0







我在寻找什么for是一些可以替换前两个之间的文本'/'



例如:



列名:ReportLocation



记录1列:/ 123 / abc / 987



记录2列: / xyz / 123/456/478



在记录1中,记录/ 123 / abc / 987应发生以下情况。 123应该替换为我添加的名称,记录2 / xyz / 123/456,xyz应该替换为我添加的名称



感谢你的负责人时间。




What I am looking for is some that can replace text between the first two '/'

example:

Column Name: ReportLocation

record 1 in Column: /123/abc/987

record 2 in Column: /xyz/123/456/478

In record 1 the following should happen record /123/abc/987. 123 should be replace with what name I add, record 2 /xyz/123/456, xyz should be hould be replace with what name I add

Thanks a head of time.

推荐答案

我不确定你是否意识到这一点,但是在SQL索引中,从1开始,而不是像C及其后继者那样用0开始。 />


尝试替换它:

I am not sure if you are aware of it, but in SQL indexes start with 1 and not with 0 as in C and its successors.

try replacing this:
REPLACE(SUBSTRING(ReportLocation, CHARINDEX('/', ReportLocation), LEN(ReportLocation)), '/', 'newone') AS ReportLocation



带有:


with this:

'/' + 'new value' + SUBSTRING(ReportLocation, CHARINDEX(ReportLocation, '/', 2), LEN(ReportLocation)) AS ReportLocation


为什么你这么困惑的兄弟?



只需一个简单的C#代码就可以了,当你访问数据时,尝试单独获取你要替换的数据(整个字符串)变量。类似



Why are you getting so confused brother?

Just a simple C# code would do that, when you access the data, try to get the data that you're trying to replace (entire string) in a seperate variable. Something like

string toReplace = "/123/abc/987"; // we'll take string in variable
string[] replacedString = toReplace.Split('/');
// 1 indexer will get the second item in the above code, '123'
replacedString[1] = "Your name here...";
// above code would update that, convert it back to string and show it!





这样做,你也可以在第二个字符串上执行相同的操作。相同的代码可以同时使用,并将替换第二个项目(/之间的那个),然后添加您想要传递的自定义名称值。



This would do it, you can execute the same on the second string too. Same code would work on both and will replace the second item (the one between /) and then add your custom name value that you want to pass.


拆分数组中的字符串,替换你想要什么,再把它粘合成一个字符串...



Split the string in an array, substitute what you want and bond it again to a string...

public void Replace1(string contents, out string output)
       {
           output = "";
           if (contents.Length > 0)
           {
               //Split the content in lines
               string[] Values = contents.Split(new char[] { '\\' });
               Values[1] = "132";
               for(int i = 0; i < Values.Length; i++)
               {
                   output +=Values[i]+"\\";
               }
           }
       }


这篇关于希望在两个'/'正斜杠之间替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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