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"