2016-03-21 7 views
0

これは最初に発生しません。インデックスの後に最初に取得する必要があります occurence。あなたが指している問題は、私がString.Replaceを使用して変更しようとしていますが、それはこれを避けるためにどのようにブランドを、見つけた場所今まで私のコードを交換しているSQLクエリを持つインデックスSQLでテーブル名を置換する

することなく、第1 出現であります

string tempString = 
    "Select t0.brandid,t0.cold,t0.colm, from brands t0 where [email protected]"; 

私はtempbrandsからとt0以前の間でテーブル名でブランドを交換したい

int tempindex1 = tempString.IndexOf("FROM "); 
string tempString1 = tempString.Replace("brands", "tempbrands "); 

期待される結果は、私はあなたが第一出現のみを変更することを疑うSelect t0.brandid,t0.cold,t0.colm, from tempbrands t0 where [email protected]

+0

初のない出現するのではなく、 "T0" を使用すると思います。インデックスの後に最初の発生を取得する必要があります。あなたが指している質問はインデックスなしの最初の発生です – Tan

+0

'IndexOf'は開始インデックスを指定するためのオーバーロードを持っています。この[MSDN](https://msdn.microsoft.com/en-us/library/7cct0x33 v = .110).aspx) – Pikoh

+0

質問を再開しました。これは、最初の部分文字列検索だけではなく、SQL解析に関するものです。 –

答えて

1

てください、私はどこから検索を開始するために指定することを修正しました。これはあなたが探していたものであるならば、私にはわからない:

string ReplaceFirst(string text, string search, string replace, string from) 
{ 
    int posFrom= text.ToLower().IndexOf(from.ToLower()); 
    if (posFrom < 0) 
    { 
      return text; 
    } 
    int pos = text.ToLower().IndexOf(search.ToLower(),posFrom); 
    if (pos < 0) 
    { 
      return text; 
    } 
    return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); 
} 

使用法:

string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM"); 

編集

以前の方法を変更することができる2つの文字列の間の文字列を置換するには以下のようにしますが、文字を限度として使用することはできません。たとえば、テーブル名に "t"が含まれていれば、コメントで尋ねる限り、 "t"を制限として使用します。

string ReplaceFirst(string text, string search, string replace, string from, string to) 
{ 

    int posFrom = text.ToLower().IndexOf(from.ToLower()); 
    if (posFrom < 0) 
    { 
     return text; 
    } 
    int posTo = text.ToLower().IndexOf(to.ToLower(),posFrom); 
    if (posTo < 0) 
    { 
     return text; 
    } 

    int pos = text.ToLower().IndexOf(search.ToLower(), posFrom); 
    if (pos < 0 ||pos >posTo) 
    { 
     return text; 
    } 
    return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); 
} 

使用法:

string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM","t0"); 
+0

@Tanを助けてくれてうれしいです:) – Pikoh

+0

"From"と "t"の間に文字列を置き換えるのを助けてください。つまり、 "from tablename t0" "あなたが正しいことをあなたに教えてください。 – Tan

+0

更新された答えを見てください。しかし、テーブル名で一致が生成される可能性があるので、1文字のパターンを使用することはできません。 – Pikoh

0

です。あなたはテーブル内での名前を変更したいと思う可能性が高いです。例えば

select brands.brandId -- <- all these three occurences should be changed: this, 
     brands.colm -- ... this 
    from brands   -- ... and this 
あなたにも select、たとえば、内の変更テーブル名をMANTこと

select b1.brandId, 
     b2.brandId 
    from brands b1, -- both tables should be changed: this 
     brands b2 -- ... and this 
    where b1.brandId > b2.brandId and 
     b1.colm = b2.colm 

任意のクエリの部分ソリューション(完全な溶液がSQLを望んでいる:(brands =>tempbrands変化する)のためにパーサー)は、正規表現によって実装できます。

String tempString = 
    "Select t0.brandid,t0.cold,t0.colm, from brands t0 where [email protected]"; 

    // let's change just whole words "brands" into "tempbrands" 
    String sql = Regex.Replace(tempString, 
          @"\bbrands\b", 
          match => "tempbrands", 
          RegexOptions.IgnoreCase); 

なぜ解決策は部分ですか? SQLは通常予想だとされるよりもマジック言語です:

select /*this brands should be left intact*/ 
     MyField as 'another brands should be spared', 
    from "this strange brands table should be skipped" 
+0

私はユーザーの入力に基づいてクエリを動的に構築し、datagridviewにバインドして、datagridviewデータをエクスポートしてExcelにエクスポートできるようにします。選択された列の数が変更されて "from"というキーワードが変更される場合があります。 "from"の後にテーブル名(ここではブランド)の最初の出現箇所を見つけて、それをシャグしてクエリを行います。 – Tan

0

は、それ以前のリンク重複を使用して簡単に...

string tempString = 
    "Select t0.brandid,t0.cold,t0.colm, from brands t0 where [email protected]"; 
tempString = tempString.Replace("from brands", "tempbrands"); 
+0

文字列クエリが定数ではなく、動的に変更されます。だから、私は最初の出現をから取ってそれを変更する必要があります。これは固定値の解決策です – Tan

関連する問題