zoukankan      html  css  js  c++  java
  • 一个可以解析嵌套IIF语句的代码

    不太常用,对于一些IIF表达式,需要转换成sql server支持的格式,就写了这个转换。反复调试之后,就可以支持嵌套的调用了。
            /// <summary>
            
    /// 转换字符串中的IIF语句
            
    /// </summary>
            
    /// <param name="?"></param>
            
    /// <returns></returns>

            static public string ChangeIIF(string oldstr)
            
    {
                
    string str=oldstr;
                str.Trim();
                
    int p0=oldstr.IndexOf("IIF");
                
    if(p0==-1)return oldstr;

                
    int p1=str.IndexOf("(",p0);

                
    //排除中间的()的干扰
                int l=0;                        //记录经过的(
                int p2=p1+1;
                
    for(;p2<str.Length ;p2++)
                
    {
                    
    if(l==0&&str.Substring(p2,1)==",")break;
                    
                    
    if(str.Substring(p2,1)=="(")l++;    //每增加一个(,l加1
                    if(str.Substring(p2,1)==")")l--;    //每增加一个(,l加1
                }

                
    if(p2==str.Length)return "";        //意外
                
    //int p2=str.IndexOf(",",p1);
                
                l
    =0;                        //记录经过的(
                int p3=p2+1;
                
    for(;p3<str.Length ;p3++)
                
    {
                    
    if(l==0&&str.Substring(p3,1)==",")break;
                    
                    
    if(str.Substring(p3,1)=="(")l++;    //每增加一个(,l加1
                    if(str.Substring(p3,1)==")")l--;    //每增加一个(,l加1
                }

                
    if(p3==str.Length)return "";        //意外
                
    //int p3=str.IndexOf(",",p2+1);    //第二个“,”

                
    //取得结束的),排除中间的()的干扰
                l=0;                        //记录经过的(
                int p4=p3+1;
                
    for(;p4<str.Length ;p4++)
                
    {
                    
    if(l==0&&str.Substring(p4,1)==")")break;
                    
                    
    if(str.Substring(p4,1)=="(")l++;    //每增加一个(,l加1
                    if(str.Substring(p4,1)==")")l--;    //每增加一个(,l加1
                }

                
    //int p4=str.Length-1;

                
    if(p4==str.Length)return "";        //意外

                
    if(p1*p2*p3*p4<=0)return oldstr;

                
    return oldstr.Substring(0,p0)+"CASE WHEN "+str.Substring(p1+1,p2-p1-1)+" THEN "+str.Substring(p2+1,p3-p2-1)+" ELSE "+str.Substring(p3+1,p4-p3-1)+" END"+oldstr.Substring(p4+1);
            }
  • 相关阅读:
    Easyui-datagrid显示时间的格式化代码
    JSP页面与JSP页面之间传输参数出现中文乱码的解决方案
    SpringMVC中在web.xml中添加中文过滤器的写法
    SpringMVC的实现过程
    BeanFactory 和 ApplicationContext的区别
    Spring中的IoC(控制反转)具体是什么东西
    Spring/AOP框架, 以及使用注解
    面向切面编程
    Spring的属性注入, byName和byType还有注入List属性
    反射, getClass(), 和something.class以及类型类(转)
  • 原文地址:https://www.cnblogs.com/jetz/p/243925.html
Copyright © 2011-2022 走看看