目的:将字符串最后4位用*代替.字符串的长度不确定,好像SUBSTRING函数不能实现吧

解决方案 »

  1.   

    从最后一位开始遍历字符串,用substring 每次取一个字符,然后replace
      

  2.   

    我觉得可以用正则表达式
    找到最后的在用replace
      

  3.   

      string   s   =   "gfdgdfgdfgdfgfd";   
        
      string reValue   =   s.substring(0,s.length   -   5   )+"****";
      

  4.   

    同意楼上的。先用substring,然后再合并就行了。
      

  5.   

    我是用SQL将结果查询出来,然后显示在GRIDVIEW控件中
    能不能直接在SQL中格式化呢,也就是说可不可以直接格式化Phone字段,以达到我的目的
    SQL: select  Phone,charge,....  from Bill  where .....能不能给出具体的代码,谢谢了!!!
      

  6.   

    string str = "SDN....";
    str = str.Substring(0,str.Length-4)+"****";//SDN****
      

  7.   

    select 
    substring(Phone,0,len(Phone)-3)+'****',
    charge
    from Bill  where ///
      

  8.   

    sql截取长度可以,替换的话不知道行不
      

  9.   

    xiaoniao_28,您好
    用你的语句在SQL中运行没问题,但运行程序的时候报错
    报错信息如下:
    IErrorInfo.GetDescription failed with E_FAIL(0x80004005).        string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += "Data Source=";
            strConnection += MapPath("database/Bill2008.mdb");
            OleDbConnection conn = new OleDbConnection(strConnection);
            string strsql = "SELECT Phone,startdate,starttime,duration,substring(Dialnumber,0,len(Dialnumber)-3)+'****' AS Dialnumber1,charge,type,destination  FROM Bill WHERE  typecode <> 9  AND  startdate  BETWEEN '" + this.txt_time_start.Text.Trim().ToString() + "' AND '" + this.txt_time_end.Text.Trim().ToString() + "' AND Phone BETWEEN '" + this.txt_phone_start.Text.Trim().ToString() + "' AND '" + this.txt_phone_end.Text.Trim().ToString() + "'";
            OleDbCommand cmd = new OleDbCommand(strsql, conn);
            conn.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.Visible = true;
            this.GridView1.DataSource = ds.Tables[0].DefaultView;
            this.GridView1.DataBind();
            conn.Close();
      

  10.   

    str.Substring( 0, str.length - 4 ) + "****"
      

  11.   


    正确如下:
    string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += "Data Source=";
            strConnection += MapPath("database/Bill2008.mdb");
            OleDbConnection conn = new OleDbConnection(strConnection);
            string strsql = "SELECT Phone,startdate,starttime,duration,left(Dialnumber,len(Dialnumber)-3)+'****' AS Dialnumber1,charge,type,destination  FROM Bill WHERE  typecode <> 9  AND  startdate  BETWEEN '" + this.txt_time_start.Text.Trim().ToString() + "' AND '" + this.txt_time_end.Text.Trim().ToString() + "' AND Phone BETWEEN '" + this.txt_phone_start.Text.Trim().ToString() + "' AND '" + this.txt_phone_end.Text.Trim().ToString() + "'";
            OleDbCommand cmd = new OleDbCommand(strsql, conn);
            conn.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.Visible = true;
            this.GridView1.DataSource = ds.Tables[0].DefaultView;
            this.GridView1.DataBind();
            conn.Close();