如何快速把JAVA转成C#

多系统对象方法都有相同的方法名,只是在大小写形式上有区别。

我们 通过一个最简单的例子:

 

用熟悉的C#,我们这么写:

using  System;  
using  System.Text;  
using  System.Text.RegularExpressions;  
namespace  LearnJavaFromCharp01  
{  
     class  Program  
    {  
         static   void  Main( string [] args)  
        {  
            String str  =   " 1ASDF NI1221312 HK1 2222/1 "   +   " 1QWW1 NI1232133 HK1 3333 " 
                 +   " 1QWW2 NI1232133 HK1 33331 "   +   " 1QWW3 NI1232133 HK1 33331/1 " 
                 +   " 1QWW4 NI1232133 HK1 33331 "   +   " 1QWW5 NI1232133 HK1 3333/1 " ;  
            String regex  =   " (1)(?:.|/n)*?///1 " ;  
            GetRegexString(str, regex);  
            Console.ReadKey();  
        }  
      static   void  GetRegexString( string  oldStr, String strPattern)  
        {  
            Regex p  =   new  Regex(strPattern, RegexOptions.Compiled);  
            MatchCollection mc  =  p.Matches(oldStr);  
             for  ( int  i  =   0 ; i  <  mc.Count; i ++ )  
            {  
                Console.WriteLine(mc[i].Value);  
            }  
        }  
    }  

using  System;
using  System.Text;
using  System.Text.RegularExpressions;
namespace  LearnJavaFromCharp01
{
     class  Program
    {

文本"1ASDF NI1221312 HK1 2222/1
1QWW NI1232133 HK1 3333/1"
两行文本,比如第1行表示旅客1的信息,第2行表示旅客2的信息,后面可能还有很多旅客信息,如何用正则表达式提取每个旅

客的信息?最后结果类似于 String1:1ASDF NI1221312 HK1 2222/1,String2:1QWW NI1232133 HK1 3333/1

         static   void  Main( string [] args)
        {
            String str  =   " 1ASDF NI1221312 HK1 2222/1 "   +   " 1QWW1 NI1232133 HK1 3333 "
                 +   " 1QWW2 NI1232133 HK1 33331 "   +   " 1QWW3 NI1232133 HK1 33331/1 "
                 +   " 1QWW4 NI1232133 HK1 33331 "   +   " 1QWW5 NI1232133 HK1 3333/1 " ;
            String regex  =   " (1)(?:.|/n)*?///1 " ;
            GetRegexString(str, regex);
            Console.ReadKey();
        }
      static   void  GetRegexString( string  oldStr, String strPattern)
        {
            Regex p  =   new  Regex(strPattern, RegexOptions.Compiled);
            MatchCollection mc  =  p.Matches(oldStr);
             for  ( int  i  =   0 ; i  <  mc.Count; i ++ )
            {
                Console.WriteLine(mc[i].Value);
            }
        }
    }
}


package  com.java.learn.csharp;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;
public   class  LearnJavaFromCsharp { public   static   void  main(String[] args) {
        String str  =   " 1ASDF NI1221312 HK1 2222/1 "   +   " 1QWW1 NI1232133 HK1 3333 "
                 +   " 1QWW2 NI1232133 HK1 33331 "   +   " 1QWW3 NI1232133 HK1 33331/1 "
                 +   " 1QWW4 NI1232133 HK1 33331 "   +   " 1QWW5 NI1232133 HK1 3333/1 " ;
        String regex  =   " (1)(?:.|/n)*?///1 " ;
        GetRegexString(str, regex);
    }
static   void  GetRegexString(String oldStr, String strPattern) {
        Pattern p=  Pattern.compile(strPattern);
        Matcher m  =  p.matcher(oldStr);
         while  (m.find())
            System.out.println(m.group());
    }
}
 

模仿着,Java可以这么写:

package  com.java.learn.csharp;  
import  java.util.regex.Matcher;  
import  java.util.regex.Pattern;  
public   class  LearnJavaFromCsharp { public   static   void  main(String[] args) {  
        String str  =   " 1ASDF NI1221312 HK1 2222/1 "   +   " 1QWW1 NI1232133 HK1 3333 " 
                 +   " 1QWW2 NI1232133 HK1 33331 "   +   " 1QWW3 NI1232133 HK1 33331/1 " 
                 +   " 1QWW4 NI1232133 HK1 33331 "   +   " 1QWW5 NI1232133 HK1 3333/1 " ;  
        String regex  =   " (1)(?:.|/n)*?///1 " ;  
        GetRegexString(str, regex);  
    }  
static   void  GetRegexString(String oldStr, String strPattern) {  
        Pattern p=  Pattern.compile(strPattern);  
        Matcher m  =  p.matcher(oldStr);  
         while  (m.find())  
            System.out.println(m.group());  
    }  

从这两段代码看,都需要添加命名空间,语法也非常类似。

Package(Java)<-->NameSpace(C#)

String(Java)<-->string(C#)

Pattern(Java)<-->Regex(C#)

Matcher(Java)<--> MatchCollection(C#)

java.util.regex(Java)<--> System.Text.RegularExpressions(C#)

 System.out.println(Java)<--> Console.WriteLine(C#)

注意:c#的String与string是唯一的一个可以大小写混用的类,不过建议还是用string才比较符合规范。

在这个例子中的几点感受:

1、开发环境Vs2010和Eclipse 3.6。共同点,巨耗内存,尤其是后者。易用性,前者明显占优。扩展性,后者遥遥领先。

编辑功能方面,Eclipse中Ctrl+1是所有程序员的最爱,它会自动提供几个备选的参考命名空间,智能吧?

 

而vs2010也有类似的功能,并且还有一些值得称赞之处,如Alt+竖选一块代码区,多好的一个功能啊。
 

2、帮助文档的实用性。MSDN绝对胜出一筹。不仅仅在中文化及时,而且示例导向性较强。各位自己比较一下:

3、开发环境的部署和生成程序的部署方面,C#易用性超出一大截,而Java要麻烦一点,毕竟是多操作系统的嘛。当然,

NetBeans IDE将易用性和功能的扩展性结合得比Eclipse要好一些,有点像vs环境了。

怎么样,对着学,是不是很轻松啊!一步一步来吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值