zoukankan      html  css  js  c++  java
  • vs2005 C#重写java.lang.String.substring()和java.lang.String.charAt()

    1.C# and Java 关于substring的比较:
       (1)先来看看有关JAVA关于substring的使用说明:
    public String substring(int beginIndex, int endIndex)


    (2)再来看看C#关于Substring的描述:
    public string Substring(int startIndex, int length);


    (3)注意第二个参数,java是endIndex,而C#中则为length

    2.在vs2005中用C#重写其后的方法(这里定义为"_substring")为:
    public static string _substring(string source,int start,int end)
        
    {
            
    string _strout = string.Empty;
            
    try
            
    {
                _strout 
    = source.Substring(0, end + 1);
            }

            
    catch(Exception e2)
            
    {
                Console.Write(e2.Message);
            }

            
    return _strout.Substring(start, _strout.Length - start - 1);
        }

    3.Java中关于charAt()的说明:


    4.为了方便说明,这里结合Substring()和charAt()以一个解密算法的过程来详细说明:
    (1)首先我们看看算法在Java中的应用:
    public static void main(String[] args) {
            
            String mw 
    = "445cdeff4543534371";//密文

            String key 
    = mw.substring(03+ mw.substring(mw.length() - 3, mw.length());

            String pass 
    = mw.substring(3, mw.length() - 3);
            
            
    int iKey1 = Integer.parseInt(key.substring(23));
            
    int iKey2 = Integer.parseInt(key.substring(12));
            
    int iKey3 = Integer.parseInt(key.substring(34));

            StringBuffer result 
    = new StringBuffer();
            
            
    for (int i = 0; i < pass.length(); i++{
              
    int tmp1 = pass.charAt(i);
              
    int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
              result.append( (
    char) tmp2);
            }
            
            System.out.println(
    " \n result :"+result.toString());//明文输出
        }

    (2)很多人都会在用C#改写charAt()时联想到IndexOf(),下面就以IndexOf()来改写举例:
    public static string decodePassword(string mw)
        
    {
            mw 
    = mw.Trim();

            
    string key = mw.Substring(03+ _substring(mw, mw.Length - 3, mw.Length-1);

            
    string pass = _substring(mw,3, mw.Length - 3);

            
    int iKey1 = Int32.Parse(_substring(key,23)); //Integer.parseInt(key.substring(2, 3));
            int iKey2 = Int32.Parse(_substring(key, 12)); //Integer.parseInt(key.substring(1, 2))
            int iKey3 = Int32.Parse(_substring(key, 34));

            StringBuilder result 
    = new StringBuilder();

            
    for (int i = 0; i < pass.Length; i++)
            
    {
                
    int tmp1 = pass.IndexOf(i.ToString());

                
    int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
                
                result.Append(tmp2.ToString());
            }


            
    return result.ToString();
        }

     
    //这里的"_substring()"方法即为上面改写的C#方法

          经过调试,当判断的对象为数字时,两种方法返回值相同,但是当判断对象中包含字符时,如上的密文为“445cdeff4543534371”时,执行结果会大不一样,经过调试发现,Java中的.charAt()转化后的结果实际是ASCII码,所以在改写C#时也要将其做相应的转化,具体变化如下:
    int tmp1 = (int)char.Parse(pass.Substring(i, 1));

    (3)分别在Eclipse和dot-Net下调试成功:
        (Eclipse)

      
       (dot-Net)


    5.总结:
    (1)C#中改写java.lang.String.substring()可用如下方法:
    public static string _substring(string source,int start,int end)
        
    {
            
    string
    _strout = string.Empty;
           
    _strout = source.Substring(0, end+1);
            
    return
    _strout .Substring(start, _strout .Length - start -1);
        }

    (2)C#中改写java.lang.String.charAt()可用如下方法:
    String.charAt(i) -> char.Parse(string.Substring(i, 1))
    (注:这里的".Substring()"为C#本身的方法定义)
  • 相关阅读:
    模糊查询三种解决方式
    trim标签&&MyBatis内置参数
    ResultMap&&鉴别器&&别名
    增加null&&使用HashMap存储查询结果集
    MyBatis处理多个参数问题
    SpringCloud中使用Zuul实现路由网关
    SpringCloud分布式config配置中心
    FeignClient的参数传递给服务提供方的方式(简单数据类型、对象)
    Hystrix的使用实例***
    什么是缓存穿透、缓存击穿
  • 原文地址:https://www.cnblogs.com/xlfj521/p/1193677.html
Copyright © 2011-2022 走看看