hpstring.java

来自「一个简单的visio程序。」· Java 代码 · 共 2,075 行 · 第 1/5 页

JAVA
2,075
字号
/*-----------------------------------------------------------------
Represent:  VBString functions

Lcase$(), Ucase$(), Left[B][$](), Right$() ,
StrComp(string1, string2[, compare] )
String$(number, character),
LSet(string1,string2), RSet(string1,string2) (not in vbs)
InStr([Start, ]string1, string2[, compare])
len( string | variant),
Mid(string, Start[, length])
Mid( string, Start[,length])=string //for statement
LTrim$(string ), RTrim$(string ), Trim$(string ),
Space$(number), total:29
Author:     Hu xia
--------------------------------------------------------------------*/

package HPCore.stdfunc;
import	java.util.*;
import  HECore.stddata.*;
import  HPCore.Exception.*;
//import  HcBean.*;

public class hpstring
{
    // for all package func
    static String get_str(Variant expression) throws HpException
    {
    String      str="";
    switch (expression.getType())
	{
	    case Variant.V_NULL:
        throw new HpException( 94, "Invalid use of null");

        case Variant.V_FIX_STR:
        fixedString     fix_str= ((VString)expression).getfixedLenStrValue();
        str = fix_str.strValue();
        break;

	    case Variant.V_STR:
	    case Variant.V_EMPTY:
	    case Variant.V_BOL:
	    case Variant.V_BYTE:
	    case Variant.V_INT:
	    case Variant.V_LONG:
	    str = expression.strValue();
	    if( str==null )
			throw new HpException(94,"Invalid use of null");
        break;

	    case Variant.V_OBJ:
        str = ((VObject)expression).getDefaultProperty().strValue();
        break;

	    case Variant.V_SINGLE:
        str = expression.strValue();
        break;

	    case Variant.V_CURR:
	    case Variant.V_DBL:
	    str = expression.strValue();

        int len=str.length();
	    if( len>=3 && str.charAt(len-1)=='0' && str.charAt(len-2)=='.' )
	        str = str.substring(0,len-2);

	    int ePos = str.indexOf("e") ;
	    if( ePos==-1 )
	    	ePos = str.indexOf("E");

	    if( ePos!=-1 )
	    {
       	    char  chr = str.charAt(ePos+1 );
            String  ss = str.substring(ePos+2);
            String ss1 = str.substring(0, ePos);
            int   digi = Integer.parseInt( ss);
            String      tmpstr="";

            if( digi<15 && chr!='-' )
            {
                int tmpi =0 ,j=0;
                for( tmpi=0; tmpi<=digi+1; tmpi++)
                {
                    if( tmpi<ss1.length() && ss1.charAt(tmpi)!='.'  )
                        tmpstr += ss1.charAt(tmpi);
                    else if( tmpi>ss1.length()-1 )
                        tmpstr += '0';
                }
                if( ss1.length()-2>digi )
                {
                    tmpstr += '.' + ss1.substring(tmpi);
                }
                str = tmpstr.toString();
            }
        }
        break;

	    case Variant.V_DATE:
	    str = expression.strValue();
	    break;

        default:
        throw new HpException(13,"Type mismatch");
	}

    return str;
    }

    static double get_db(Variant expression) throws HpException
    {
    double   db=0;
    
    switch (expression.getType())
	{
    	case Variant.V_NULL:
	    throw new HpException(94,"Invalid use of null");


    	case Variant.V_STR:
    	String s = expression.strValue();
    	if(s==null )
    		throw new HpException(94,"Invalid use of null");
    	s = s.trim();
    	if( s.compareTo("")==0 )
    	    throw new HpException(13, "Type mismatch");

	    db = strtodbl( s );
    	break;

	    case Variant.V_FIX_STR:
        fixedString     fix_str= ((VString)expression).getfixedLenStrValue();
        String  str = fix_str.strValue();
        if(str==null )
    		throw new HpException(94,"Invalid use of null");
    	str = str.trim();
    	if( str.compareTo("")==0 )
    	    throw new HpException(13, "Type mismatch");

   	    db = strtodbl(str);
       	break;

    	case Variant.V_EMPTY:
    	case Variant.V_BOL:
    	case Variant.V_BYTE:
    	case Variant.V_INT:
    	case Variant.V_LONG:
    	case Variant.V_DATE:
    	case Variant.V_CURR:
    	case Variant.V_DBL:
    	case Variant.V_SINGLE:
        db = expression.dblValue() ;
    	break;

	    case Variant.V_OBJ:
		db = ((VObject)expression).getDefaultProperty().dblValue();
        break;

    	default:
	    throw new HpException(13,"Type mismatch");

        }
        return db;
    }

    public static double strtodbl( String s ) throws HpException
    {
        double   db=0;
    	Double  isdb;
    	try
    	{
    	    isdb = new Double(s);
    	    db = isdb.doubleValue();
    	    if( s.charAt(s.length()-1 )=='d' || s.charAt(s.length()-1 )=='D' )
    	       throw new HpException(13,"Type mismatch");

    	}catch( Exception err)
        {
    	    if( s.length()<2 || ( s.charAt(s.length()-1 )=='d' || s.charAt(s.length()-1 )=='D') )
    	    	throw new HpException( 13, "Type mismatch");

    	    if( s.charAt(0)=='$' )
    		    s = s.substring(1);
    	    else if( s.charAt(s.length()-1)=='$' )
    		    s = s.substring(0, s.length()-1 );

        	if( s.length()>2 && s.charAt(0)=='(' && s.charAt(s.length()-1)==')' )
        	    s = "-" + s.substring(1, s.length()-1) ;

    	    try
    	    {
        		isdb = new Double(s);
        		db = isdb.doubleValue();
    	    }catch( Exception err1)
        	{
    		    if( s.length()==2 )
    		    {
    	    	    if( s.charAt(0)=='&')
    		    		s = s.substring(1);
        		    try
        		    {
            		    db = (double)Long.parseLong(s, 8);
           		    } catch( Exception err2 )
        		    {
            			throw new HpException( 13, "Type mismatch");
        			}
    		    }
        		else if( (s.charAt(0)=='-' || s.charAt(0)=='+') &&
        		    	    s.charAt(1)=='$'    )
        		{
        		    if( s.charAt(0)=='-' )
        			    s = '-' + s.substring(2);
        		    else
        			    s = s.substring(2);

        		    try
        		    {
        				isdb = new Double(s);
        				db = isdb.doubleValue();
        		    }catch( Exception err3)
        		    {
        			    throw new HpException( 13, "Type mismatch");
        			}
        		}
        		else if( (s.charAt( s.length()-1 )=='-' || s.charAt( s.length()-1 )=='+') &&
        			  s.charAt(s.length()-2 )=='$' )
        		{
        		    if( s.charAt(s.length()-1)=='-' )
        			    s = '-' + s.substring(0,s.length()-2 );
        		    else
        			    s = s.substring(0,s.length()-2 );

        		    try
        		    {
        				isdb = new Double(s);
        				db = isdb.doubleValue();
        		    }catch( Exception err4)
        			{
        			    throw new HpException( 13, "Type mismatch");
        			}
        		}
        		else if( s.charAt(0)=='&' && (s.charAt(1)=='h' || s.charAt(1)=='H' ) )
        		{
        		    try
        		    {
        			    s = s.substring(2);
        			    db = (double)Long.parseLong(s, 16);
        		    }catch( Exception err5)
        		    {
        				throw new HpException( 13, "Type mismatch");
        		    }
        		}
        		else if( s.charAt(0)=='&' && (s.charAt(1)=='o' || s.charAt(1)=='O' ) )
        		{
        		    try
        		    {
        			    s = s.substring(2);
        			    db = (double)Long.parseLong(s, 8);
        		    }catch( Exception err6)
        		    {
        			    throw new HpException( 13, "Type mismatch");
        			}
        		}
        		else if( s.charAt(0)=='&' && (s.charAt(1)>='0' && s.charAt(1)<='9' ) )
        		{
        		    try
        		    {
        			    s = s.substring(1);
        			    db = (double)Long.parseLong(s, 8);
        		    }catch( Exception err7)
        		    {
        			    throw new HpException( 13, "Type mismatch");
        			}
        		}
        		else
        		{
        		    int le = s.length() , found=0;
        		    String   s1="";
        		    boolean  dot=false, comma = false, char_e=false;
        		    for (found = 0; found < le; found++)
        		    {
    		            if( s.charAt(found)=='.' && (dot==true || char_e==true))   break;
    		            if( s.charAt(found)==',' && (found==0 || char_e==true ))   break;
    		            if( s.charAt(found)=='e' && (char_e==true || found==0))   break;

    		            if ( s.charAt(found)=='.')
    		            {
    		                dot = true;
    		            }
    		            else if( s.charAt(found)==',')
    		            {
    		                comma = true;
    		                continue;
    		            }
    		            else if( s.charAt(found)=='e' || s.charAt(found)=='d' )
    		            {
    		                char_e = true;
    		                if( s.charAt( found)=='d' )
    		                {
    		                    s1 += 'e' ;
    		                    continue;
    		                }
    		            }
    		            else if(s.charAt(found)<'0' && s.charAt(found)>'9' )
    		                break;

		                s1 +=s.charAt(found);
	    	        }

	    	        s = s1;
	    	        if( found==le )
        		    {
        		        try
        		        {
        				    isdb = new Double(s);
            				db = isdb.doubleValue();
            			}catch( Exception r )
            			 {
            			    throw new HpException(13, "Type mismatch.");
            			 }
        		    }//end of found==le
        		    else
        		    {
       		            throw new HpException( 13, "Type mismatch");
       		        }
        		}
    	    }//end of err1
    	}
    	return db;
    }

    /**
     * LCase(String)
     * Returns a string that has been converted to lowercase.
     * The string argument is any valid string expression. If string contains Null, Null
       is returned.
     * Remarks
        Only uppercase letters are converted to lowercase; all lowercase letters and nonletter characters remain unchanged.
     */
    public static String LCASE$(Variant varstr) throws HpException
    {
        String   str = get_str(varstr);
    	return str.toLowerCase();
    }

    public static String LCASE$(boolean bool_str) throws HpException
    {
        Boolean  bol = new Boolean(bool_str);
    	return bol.toString().toLowerCase();
    }

    public static String LCASE$(HByte byt_str) throws HpException
    {
    	return byt_str.strValue().toLowerCase();
    }

    public static String LCASE$(short sh_str) throws HpException
    {
    	return Short.toString(sh_str).toLowerCase();
    }

    public static String LCASE$(int int_str) throws HpException
    {
    	return Integer.toString(int_str).toLowerCase();
    }

    public static String LCASE$(float f_str) throws HpException
    {
    	return Float.toString(f_str).toLowerCase();
    }

    public static String LCASE$(double d_str) throws HpException
    {
    	return Double.toString(d_str).toLowerCase();
    }
    
    public static String LCASE$(HCurr vc_str) throws HpException
    {
        double  d = vc_str.dblValue();
    	return Double.toString(d).toLowerCase();
    }

    public static String LCASE$(HDate dt_str) throws HpException
    {
    	return dt_str.strValue().toLowerCase();
    }

    public static String LCASE$(fixedString fixedstr) throws HpException
    {
        return fixedstr.strValue().toLowerCase();
    }

    public static String LCASE$(String str) throws HpException
    {
    	return str.toLowerCase();
    }


    /*--- FUNCTION: LCase(string) ---*/
    public static Variant LCASE(Variant varstr) throws HpException
    {
        String      str="";
    
    	switch (varstr.getType())
	    {
        case Variant.V_NULL:
        return new VNull();

	    case Variant.V_FIX_STR:
        fixedString  fix_str = ((VString)varstr).getfixedLenStrValue();
        str = fix_str.strValue();
        break;

	    case Variant.V_STR:
	    case Variant.V_EMPTY:
	    case Variant.V_BOL:
	    case Variant.V_BYTE:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?