如何替换第一个和最后一个字符

时间:2019-02-20 07:10:43

标签: c#

我想替换@param,但我想知道@param中的第一个和最后一个字符

//declare variable
string source = "select * from table where tablename like '%@param%'";
string replacestring = "'admin'";

//check before replace
if(check before @param have % or last @param have %)
{
    source.Replace("'", string.Empty);
    source.Replace("@param", replacestring);
}
else
{
    source.Replace("@param", replacestring);
}

2 个答案:

答案 0 :(得分:0)

您可以将String.StartsWith()String.EndsWith()用作:

string str = "%param%";
if (str.StartsWith("%") || str.StartsWith("%"))
{
    // Do something
}

答案 1 :(得分:0)

一般情况下,您想要解析:如果有

ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2/Users/xxx/.rbenv/versions/2.5.3/bin/ruby -I /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/site_ruby/2.5.0 -r ./siteconf20190220-84910-2gf7i1.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.7/lib
-----
creating Makefile
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/xxx/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2

查询?但是,如果可以保证查询足够简单,则可以尝试使用正则表达式

select 'bla-bla-bla %@DoNotChangeThis% string' -- string within query
  from MyTable

结果:

  string source = "select * from table where tablename like '%@param%'";

  // Let's generalize solution: 
  //   1. Get all @param@ matches
  //   2. Change them according their names
  string result = Regex.Replace(
      source,
    @"(?<=%)@[A-Za-z][A-Za-z0-9_]*(?=%)",
      match => 
        match.Value == "@param" ? "admin"        // @param to "admin" 
                                : match.Value);  // unknown - do not change

  Console.Write(result);