0

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

In my SQL Database, have a data:

Reason:%s与%s等预算科目的内容存在重复,核减%d万元

ResultS1:本科目镍铁中多元素样品前处理

ResultS2:本科目ICP测定法

ResultD:2.50

I want get a string:本科目镍铁中多元素样品前处理与本科目ICP测定法等预算科目的内容存在重复,核减2.50万元

Bassie
  • 9,529
  • 8
  • 68
  • 159
Foskill
  • 81
  • 1
  • 9
  • 1
    use string interpolation : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated – Bassie May 03 '18 at 06:04
  • 1
    @sLw You would do it in the same way you do it with `string.Format` - you process the data before interpolating – Bassie May 03 '18 at 06:09
  • 1
    @Bassie but why would you first use string.remove and then interpolate? that's hardcoding at its best – slow May 03 '18 at 06:10
  • 1
    Maybe you could actually explain the rules of what you're trying to achieve, rather than just providing a single example in a language that will likely be unfamiliar to most. There are a hundred ways and more to get that result from that data but, unless we know what the actual rules are, we have no idea whether they would work for other data. In short, provide a FULL and CLEAR explanation of the problem. – jmcilhinney May 03 '18 at 06:14
  • Thank you for your answer, but I still don't understand how to achieve it. – Foskill May 03 '18 at 06:15
  • 1
    @Foskill just use Regex.Replace() and you are good to go. You can specify that it only replaces the first occurrence. – slow May 03 '18 at 06:20
  • @sLw Thank you for giving me a try again! – Foskill May 03 '18 at 06:27

2 Answers2

1
SELECT concat_ws('',ResultS1,'与',ResultS2,'等预算科目的内容存在重复,核减',ResultD,'万元') as Result 
FROM `yourtable`

Follow the answer concatenate string in c#

string key = String.Join("", new String[] { ResultS1,'与',ResultS2,'等预算科目的内容存在重复,核减',ResultD,'万元'});
Ryuk Lee
  • 720
  • 5
  • 12
  • and how are you replaceing the %s? this is way too hardcoded – slow May 03 '18 at 06:06
  • Thank you for your answer, but have you implemented it in C#? – Foskill May 03 '18 at 06:14
  • Thank you for your answer, but my data [Reason] is var.This will not be realized. – Foskill May 03 '18 at 06:27
  • 1
    I think in c# you need to change `%s` and `%d` to parameters `{0}` , `{1}` , `{2}` and try [that way](https://stackoverflow.com/questions/1434059/c-sharp-string-format-args) :D. Hope that helps – Ryuk Lee May 03 '18 at 06:28
1

You can use the helpful extension method from this question (second answer down) ReplaceFirst:

public static string ReplaceFirst(this string text, string search, string replace)
{
    int pos = text.IndexOf(search);
    if (pos < 0)
    {
        return text;
    }
    return $"{text.Substring(0, pos)}{replace}{text.Substring(pos + search.Length)}";
}

and then call it like this

var reason = "%s与%s等预算科目的内容存在重复,核减%d万元";
var results1 = "本科目镍铁中多元素样品前处理";
var results2 = "本科目ICP测定法";
var resultd = "2.50";

var final = reason
    .ReplaceFirst("%s", results1)
    .ReplaceFirst("%s", results2)
    .ReplaceFirst("%d", resultd);

Where final is assigned

"本科目镍铁中多元素样品前处理与本科目ICP测定法等预算科目的内容存在重复,核减2.50万元"
Bassie
  • 9,529
  • 8
  • 68
  • 159