• Java(String)


    1>String类型判等

    (1)请运行以下示例代码StringPool.java查看其输出结果。如何解释这样的输出结果?从中你能总结出什么?

     1 package StringPool;
     2 public class StringPool {
     3     
     4     public static void main(String args[])
     5     {
     6         
     7         String s0="Hello";
     8         
     9         String s1="Hello";
    10         
    11         String s2="He"+"llo";
    12         
    13         System.out.println(s0==s1);//true
    14         
    15         System.out.println(s0==s2);//true
    16         
    17         System.out.println(new String("Hello")==new String("Hello"));//false  
    18         
    19     }
    20 }

    运行结果:

    实验分析:

    1、在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0,s1,s2实际上引用的是同一个对象,所以判断值为true。

    2、当直接使用 new 关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象,所以判断值为false。

    3、判断两个对象是否一致是判断两对象的内容和地址是否一致。

    (2)再看……为什么会有上述的输出结果?从中你又能总结出什么?

     1 public class Text1 {
     2 
     3     public static void main(String args[])
     4     {
     5         
     6         String s1="a";
     7         String s2=s1;
     8         System.out.println(s1==s2);
     9         s1+="b";
    10         System.out.println(s1==s2);
    11         System.out.println(s1=="ab");
    12         System.out.println(s1.equals("ab"));
    13     }
    14 }

    实验结果:

    实验分析:

    1、由于s2 = s1,所以两者引用的是同一个对象,所以s1==s2判断值为true

    2、String对象的内容是只读的,修改了 s1 变量的值,实际上是得到了一个新的字符串对象,其内容为 “ab”,它与原先 s1 所引用的对象 ”a” 无关,所以,s1==s2返回false

    3、而“ab”字符串是一个常量,常量有单独的存储空间,它所引用的字符串与 s1 所引用的 “ab” 对象无关,所以判断值为 false

    4、String.equals()方法可以比较两个字符串的内容。

    2>请查看String.equals()方法的实现代码,注意学习其实现方法。

    public class StringEquals {
    
        public static void main(String[] args) {
            
            String s1=new String("Hello");
            
            String s2=new String("Hello");
    
            
            System.out.println(s1==s2);
            
            System.out.println(s1.equals(s2));
    
            
            String s3="Hello";
            
            String s4="Hello";
    
              
            System.out.println(s3==s4);
            
            System.out.println(s3.equals(s4));
              
        }
    }

    实验结果:

    实验分析:

    1、直接使用new关键字创建字符串对象时(s1和s2),虽然值一致(都是“Hello”),但仍然是两个独立的对象,所以用“==”判断时判断值为false

    2、String.equals()用于判断值是否相等,值都为“hello”,所以判断值为true

    3>字串加密

    古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:

    请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图。

    设计思想:加密即是将输入字符串的每个字符向后移三个位置A-W对应的ACCII码加3,X-Z减23,但要注意强制转化为char类型;解密类似,D-Z对应的ACCII码减3,A-C加23。

    程序流程图:

    源代码:

     1 import java.util.*;
     2 //20153220 曹婷婷 2016.10.28
     3 public class JiaMi {
     4     public static void main(String[] args) {
     5         Scanner in = new Scanner(System.in);
     6         System.out.println("请输入要进行的操作:1、加密 2、解密");
     7         int m = in.nextInt();
     8         String result = "";
     9         if(m == 1)
    10         {
    11             System.out.println("请输入要加密的字符串(大写字母)");
    12             String str = in.next();
    13             for(int i =0;i<str.length();i++)
    14             {
    15                 if(str.charAt(i)>= 65 &&str.charAt(i)<=87 )
    16                     {result += (char)(str.charAt(i) + 3);}
    17                 else if(str.charAt(i)>= 87 &&str.charAt(i)<=90 )
    18                     {result += (char)(str.charAt(i) - 23);}
    19                 else
    20                 {
    21                     System.out.println("输入错误!");
    22                     break;
    23                 }
    24             }
    25             System.out.println("加密后字符串为"+result);
    26         }
    27         else if(m == 2)
    28         {
    29             System.out.println("请输入要解密的字符串(大写字母)");
    30             String string = in.next();
    31             for(int i =0;i<string.length();i++)
    32             {
    33                 if(string.charAt(i)>= 68 &&string.charAt(i)<=90 )
    34                     {result += (char)(string.charAt(i) - 3);}
    35                 else if(string.charAt(i)>= 65 &&string.charAt(i)<=67 )
    36                     {result += (char)(string.charAt(i) + 23);}
    37                 else
    38                 {
    39                     System.out.println("输入错误!");
    40                     break;
    41                 }
    42             }
    43             System.out.println("解密后字符串为"+result);
    44         }
    45         else
    46         {
    47             System.out.println("输入错误!");
    48         }
    49     }
    50 }

    结果截图:

    4>整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明

      Length():获取字符串长度

     charAt():获取指定位置的字符

     getChars():获取从指定位置起的子串复制到字符数组中,四个参数:

    1.  第一个参数指出了字符串截取所开始的位置;  
    2.  第二个参数指出了字符串截取所结束的位置;  
    3.  第三个参数指出目标(即接收)字符数组;  
    4.  第四个参数指出目标字符数据接收的开始下标。   

     replace():子串替换

         replace(char oldChar, char newChar)
         返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。

     toUpperCase()、 toLowerCase():大小写转换

      trim():去除头尾空格

     toCharArray():将字符串对象转换为字符数组

  • 相关阅读:
    如何找到一本好书以及读一本好书
    笔记
    web基础知识(二)
    7RestClient操作索引库
    3索引库操作
    4文档操作
    5实战hotel表创建es映射
    6初始化RestClient
    kafka的生产者学习
    kafka的缺陷
  • 原文地址:https://www.cnblogs.com/ttzz/p/6007527.html
Copyright © 2020-2023  润新知