Because Python is very familiar with the implementation of the Java implementation of the various types of string operations, and then the implementation of the Java method and the way the Python is written down.
Let's start with the summary, the Java string class itself defines some simple string operations,
String common operations are:
1. Take a character first occurrence/last occurrence index
2. Take a character from a position in a string
3. String interception
4. Remove the leading and trailing spaces
5. String character substitution
6. Determine whether two strings are equal
7. Case conversion
Start here:
1. Take a character first occurrence/last occurrence index
Java |
Python |
1 " Span style= "COLOR: #0000ff" >public class Test { 2 public static void main (String args[]) { 3 String s = "Hello,world." 4 System.out.println (S.indexof ("E" 5 System.out.println (S.lastindexof ("L" 6 } 7< /span>} |
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 a = "HelloWorld"4 Print A.find ("L")5 print a.rfind ("L") |
Explain:
String content lookups in Java and Python are similar, and are built-in methods that use strings directly.
In fact, the root cause is that the strings in both languages are an instantiated class.
IndexOf (str) and Latsindexof (str) in Java
Find (str) in Python with RFind (str).
2. Take a character from a position in a string
Java |
Python |
1 " Span style= "COLOR: #0000ff" >public class Test { 2 public static void main (String args[]) { 3 String s = "Hello,world." 4 System.out.println (S.charat (4 5 } 6< /span>} |
1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 " HelloWorld " 4 print a[5] |
Explain:
Java uses string to bring the method Charat (index)
Python uses the slicing method to intercept the string directly from the index, or to take it directly, just like a value in an array.
In fact, there is a growing sense of the elegance and simplicity of python.
3. String interception
Java |
Python |
1 " Span style= "COLOR: #0000ff" >public class Test { 2 public static void main (String args[]) { 3 String s = "Hello,world." 4 System.out.println (s.substring (2,55 } 6 } |
1 " Span style= "COLOR: #008000" ># !/usr/bin/env python 2 # -*- coding:utf-8-*- 3 a = " helloworld " 4 Span style= "COLOR: #0000ff" >print a[1:3] 5 print a[1:] 6 print a[: ] 7 print a[-1:] |
Explain:
The process of string interception is to copy part of the original string.
Java uses method substring (indexbegin,indexend) to intercept strings.
Python still uses the method of string slicing, various fancy interception.
In fact, if Java really wants to deal with complex strings, one is to use regular, one is to split the string after splitting the array. This is similar to Python when working with arrays.
I think Python is really amputated Java here. Well, you shouldn't say that, this article is not a comparison of the merits of two languages.
4. Remove the leading and trailing spaces
Java |
Python |
Public class Test {2 Public Static void Main (String args[]) {3 String s = "Hello,world"; 4 System.out.println (S.trim ()); 5 }6} |
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3A ="HelloWorld"4b ="hellowrold\n"5 PrintA.strip (" ")6 PrintB.strip ("\ n") |
Java calls the String method trim () directly, removing the leading and trailing spaces.
Python is similar, using the Strip () method, but supports the removal of input parameters.
5. String substitution
Java |
Python |
Public class Test {2 Public Static void Main (String args[]) {3 String s = "Hello,world"; 4 System.out.println (S.replace ("E", "a")); 5 }6} |
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportRe4A ="HelloWorld"5b ="Hellowrold"6 PrintA.replace ("L","")7 PrintRe.sub ("L",""A |
Java using the String method replace
Python can also use the Replace method. Or use the RE module.
6. Judging string equality
Java |
Python |
1 Public classTest {2 Public Static voidMain (String args[]) {3String str1 = "Hello,world";4String str2 = "Hello,world";5String STR3 =NewString ("Hello,world");6String STR4 =STR3;7System.out.println ("str1 equals str2:" +str1.equals (str2));8System.out.println ("str1 = = str2:" + (str1 = =str2));9System.out.println ("str2 equals STR3:" +str2.equals (STR3));TenSystem.out.println ("str2 = = Str3:" + (str2==str3)); OneSystem.out.println ("Str3 equals STR4:" +str3.equals (STR4)); ASystem.out.println ("STR3 = = Str4:" + (str3==STR4)); - } -} 1 str1 equals str2:true2 str1 = = str2:true3 str2 equals STR3:true 4 str2 = = Str3:false5 str3 equals STR4:true6 str3 = = Str4:true
|
1 " HelloWorld " 2 " HelloWorld " 3 Print (a==B) 4 Print is b) Truetrue |
In Java, this paragraph is actually said.
The Equals () method compares the contents of the object, and the "= =" Comparison is the same object.
Content, STR1=STR2=STR3=STR4 not have to say. But str1 and str2 are the same object, STR3 and STR4 are the same object. STR2 and STR3 are not the same object.
Here comes the question of Java references and object creation, which is discussed in detail in the next issue.
7. Case conversion
Java |
Python |
1 Public class Test {2 Public Static void Main (String args[]) {3 New String ("Hello,world"); 4 System.out.println (Str1.touppercase ()); 5 System.out.println (Str1.tolowercase ()); 6 }7 }
|
" HelloWorld " Print (A.capitalize ()) Print (A.lower ()) Print (A.upper ()) |
Both Java and Python use a string-built method for case conversion.
But Python has one more capitalize () method for first-letter capitalization. Java words need to write a method to achieve.
String common operations are these, all of which are the methods of the string class.
IndexOf (char), lastIndexOf (char) searches for the position of the character;
CharAt (index) takes the character by index;
SUBSTRING (begin,end) intercepts sub-strings by index;
Trim () Remove the leading and trailing spaces;
Replace (CHARA,CHARB) replaces all CharA with Charb
Equals (CharA) determines whether it is equal to CharA. = = is used to determine whether two objects are the same.
The next article is a study of Java references and objects. Then we talk about the reference and the object to say Java heap and stack space.
Java Getting Started learning Note 2 (string manipulation in Java)