java入门学习笔记之2(Java中的字符串操作)

因为对Python很熟悉,看着Java的各种字符串操作就不自觉的代入Python的实现方法上,于是就将Java实现方式与Python实现方式都写下来了。

先说一下总结,Java的字符串类String本身定义了一些简单的字符串操作,

字符串常用操作有:

1. 取某一字符第一次出现/最后一次出现的索引

2. 取字符串某一位置的字符

3. 字符串截取

4. 去除首尾空格

5. 字符串字符替换

6. 判断两个字符串是否相等

7. 大小写转换

下面开始:

1.取某一字符第一次出现/最后一次出现的索引

JAVAPython
1 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 }

 

 

1 #!/usr/bin/env python
2 # -*-coding:utf-8-*-
3 a = "HelloWorld"
4 print a.find("l")
5 print a.rfind("l")

 

 

 

 

 

 

 

 

 

 

 

 解释:

Java和Python中的字符串内容查找类似,都是直接使用字符串的内置方法。

其实根本原因是这两种语言中字符串都是一个实例化的类。

Java中的indexOf(str)与latsIndexOf(str)

Python中的find(str)与rfind(str)。

 

2. 取字符串某一位置的字符

JavaPython
1 public class Test {
2         public static void main(String args[]){
3                 String s = "Hello,World.";
4                 System.out.println(s.charAt(4));
5                 }
6 }

 

 
1 #!/usr/bin/env python
2 # -*-coding:utf-8-*-
3 a = "HelloWorld"
4 print a[5]

 

 

 

 

 

 

 

 

 

解释:

Java使用字符串自带的方法charAt(index)

Python使用 切片方法,直接通过索引将字符串截取出来,或者说就像取数组中的某个值一样直接取出来。

其实写到这里越来越体会到Python的优雅和简洁。

 

3. 字符串截取

JavaPython
1 public class Test {
2         public static void main(String args[]){
3                 String s = "Hello,World.";
4                 System.out.println(s.substring(2,5));
5                 }
6 }

 

1 #!/usr/bin/env python
2 # -*-coding:utf-8-*-
3 a = "HelloWorld"
4 print a[1:3]
5 print a[1:]
6 print a[:]
7 print a[-1:]

 

 

 

 

 

 

 

 

 

 

 

解释:

字符串截取的过程就是将原字符串的一部分复制出来。

Java使用方法substring(indexbegin,indexend)来截取字符串。

Python仍然使用字符串切片的方法,各种花式截取。

其实Java如果真的想处理复杂字符串,一个是用正则,一个是将字符串split分割后处理数组。当处理数组时候和python就很类似了。

觉得这里Python真的吊打Java了。好了不应该说这些的,这篇文章又不是比较两个语言的优劣。

 

4.去除首尾空格

JavaPython
1 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 python
2 # -*-coding:utf-8-*-
3 a = "  HelloWorld  "
4 b = " HelloWrold\n"
5 print a.strip(" ")
6 print b.strip("\n")

 

 

 

 

 

 

 

 

 

 

Java直接调用字符串方法trim(),去除首尾空格。

Python类似,使用strip()方法,但是支持按输入的参数去除。

 

5. 字符串替换

JavaPython
1 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 python
2 # -*-coding:utf-8-*-
3 import re
4 a = "HelloWorld  "
5 b = "HelloWrold"
6 print a.replace("l","")
7 print re.sub("l","",a)

 

 

 

 

 

 

 

 

 

 

 

Java使用字符串方法replace

Python也可以使用replace方法。或者使用re模块。

6. 判断字符串相等

JavaPython
 1 public class Test {
 2         public static void main(String args[]){
 3                 String str1 = "Hello,World";
 4                 String str2 = "Hello,World";
 5                 String str3 = new String("Hello,World");
 6                 String str4 = str3;
 7                 System.out.println("str1 equals str2:"+str1.equals(str2));
 8                 System.out.println("str1 == str2:"+(str1 == str2));
 9                 System.out.println("str2 equals str3:"+str2.equals(str3 ));
10                 System.out.println("str2 == str3:"+(str2==str3));
11                 System.out.println("str3 equals str4:"+str3.equals(str4));
12                 System.out.println("str3 == str4:"+(str3==str4));
13                 }
14 }
1 str1 equals str2:true
2 str1 == str2:true
3 str2 equals str3:true
4 str2 == str3:false
5 str3 equals str4:true
6 str3 == str4:true

 

1 a = "HelloWorld"
2 b = "HelloWorld"
3 print (a==b)
4 print (a is b)
True
True

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Java中其实这一段有的说的。

equals()方法比较对象的内容,"=="比较是否是同一个对象。

内容上,str1=str2=str3=str4没得说。但是str1和str2是同一个对象,str3和str4是同一个对象。str2和str3并不是同一个对象。

这里就涉及到Java的引用与对象创建的问题,下一期在详细讨论。

7. 大小写转换

JavaPython
1 public class Test {
2         public static void main(String args[]){
3                 String str1 = new String("Hello,World");
4 System.out.println(str1.toUpperCase()); 5 System.out.println(str1.toLowerCase()); 6 } 7 }

 

1 a = "helloworld"
2 print (a.capitalize())
3 print (a.lower())
4 print (a.upper())

 

 

 

 

 

 

 

 

 

 

 

Java和Python都使用了字符串内置方法进行大小写转换。

不过Python多了一个capitalize()方法进行首字母大写的处理。Java的话需要自己写一个方法去实现。

 

字符串常见操作就是这些,都是String类自带的方法。

indexOf(char),lastIndexOf(char)搜索字符的位置;

charAt(index)按索引取字符;

substring(begin,end)按索引截取子串;

trim()去除首尾空格;

replace(charA,charB)将所有charA替换为charB

equals(charA)判断是否与charA相等。==用来判断两个对象是否是同一个。

 

下一篇就Java的引用与对象做个研究。然后谈到了引用与对象就要说一下Java的堆与栈空间。

 

转载于:https://www.cnblogs.com/lijianyang/p/7042198.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值