How can Java be quickly compiled into C #? (1): similar methods

Source: Internet
Author: User

I plan to take some time every day to record the Java learning process. This intermittent Java learning note is a sporadic Summary of the java learning process for backup. At the same time, I hope to learn from CSharp's friends in the Java field.

In contrast, C # Is a postmaster, which absorbs most of the essence of Java, but there are still some nuances between the two. Today, let's look at the first difference:

■ Many System Object methods have the same method name, but they are different in case format.

Here is a simple example:

Text "1 ASDF ni1231612 HK1 2222/1
1QWW ni12321_hk1 3333/1"
Two lines of text, such as 1st lines, indicate the information of passenger 1, and 2nd lines indicate the information of passenger 2. There may be a lot of passenger information. How can we extract the information of each passenger using a regular expression? The final result is similar to String1: 1 ASDF ni1231612 HK1 2222/1, String2: 1QWW ni12321_hk1 3333/1


Using the familiar C #, let's write:

Using System;
Using System. Text;
Using System. Text. RegularExpressions;
Namespace LearnJavaFromCharp01
{
Class Program
{
Static void Main (string [] args)
{
String str = "1 ASDF ni1231612 HK1 2222/1" + "1QWW1 ni12321_hk1 3333"
+ "1QWW2 ni12321_hk1 33331" + "1QWW3 ni12321_hk1 33331/1"
+ "1QWW4 ni12321_hk1 33331" + "1QWW5 ni12321_hk1 3333/1 ";
String regex = "(1 )(? :. | )*? /\ 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
{
Static void Main (string [] args)
{
String str = "1 ASDF ni1231612 HK1 2222/1" + "1QWW1 ni12321_hk1 3333"
+ "1QWW2 ni12321_hk1 33331" + "1QWW3 ni12321_hk1 33331/1"
+ "1QWW4 ni12321_hk1 33331" + "1QWW5 ni12321_hk1 3333/1 ";
String regex = "(1 )(? :. | )*? /\ 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 );
}
}
}
}

It can be written in this way in 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 = "1 ASDF ni1231612 HK1 2222/1" + "1QWW1 ni12321_hk1 3333"
+ "1QWW2 ni12321_hk1 33331" + "1QWW3 ni12321_hk1 33331/1"
+ "1QWW4 ni12321_hk1 33331" + "1QWW5 ni12321_hk1 3333/1 ";
String regex = "(1 )(? :. | )*? /\ 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 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 = "1 ASDF ni1231612 HK1 2222/1" + "1QWW1 ni12321_hk1 3333"
+ "1QWW2 ni12321_hk1 33331" + "1QWW3 ni12321_hk1 33331/1"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.