zoukankan      html  css  js  c++  java
  • 4String

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

    请编写一个程序,使用上述算法加密或解密用户输入的英文字串

    1、设计思想

    输入字符串,使用判断其字符串长度,用charAt()将它们转换为字符,然后判断当(c>='X'&&c<='Z')使c=c-23,当(c>='A'&&c<='W')c=c+3,之后即可解密。

    2、程序流程图

    3、源代码

     1 import java.util.*;
     2 public class SecretString {
     3  
     4 public static void main(String args[])
     5 {
     6 Scanner scanner=new Scanner(System.in);
     7 System.out.println("请输入字符串:");
     8 String str=scanner.nextLine();
     9 String output="";
    10 char c = 0;
    11 for(int i=0;i<str.length();i++)
    12 {
    13 c=(str.charAt(i));
    14 {
    15 if(c>='X'&&c<='Z')
    16 c-=23;
    17 else if(c>='A'&&c<='W')
    18 c+=3;
    19 }
    20 output+=c;
    21 }
    22 System.out.println("加密的字符串结果为:
    "+output);
    23 }
    24 }
    25  

    4、结果截图

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

    public class StringPool {
       
           public static void main(String args[])
        {
           
                 String s0="Hello";
           
                 String s1="Hello";
           
                 String s2="He"+"llo";
           
                 System.out.println(s0==s1);//true
           
                 System.out.println(s0==s2);//true
           
                 System.out.println(new String("Hello")==new String("Hello"));//false
           
           }
    }

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

    2.编译器在编译s2一句时,会去掉“+”号,直接把两个字串连接起来得一个字串(“Hello”)。这种优化工作由Java编译器自动完成。

    3.当直接使用new关键字创建字符串对象时,虽然值一致(都是“Hello”),但仍然是两个独立的对象。

     

    为什么会有上述的输出结果?从中你又能总结出什么?
     
    给字串变量赋值意味着:两个变量(s1,s2)现在引用同一个字符串对象“a”!
    String对象的内容是只读的,使用“+”修改s1变量的值,实际上是得到了一个新的字符串对象,其内容为“ab”,它与原先s1所引用的对象”a”无关,所以,s1==s2返回false;
    代码中的“ab”字符串是一个常量,它所引用的字符串与s1所引用的“ab”对象无关。String.equals()方法可以比较两个字符串的内容。
    三、字符串比较
    使用equals()或equalsIgnoreCase()方法比较两字串内容是否相同,equalsIgnoreCase忽略大小写,使用==比较两字符串变量是否引用同一子串对象
    compareTo:使用字典法进行比较,返回0表两字串相等,小于返回负值,大于返回正值
    regionMatches:比较两字串中的某一部分是否相等
    参数含义如下:
          1.调用这个方法的字串中的起始下标
          2.要比较的字串
          3.要比较的字串的起始下标
          4.要比较的字串比较的字符长度
    比如s3.regionMatches( 0, s4, 0, 5 ),若忽略大小写使用s3.regionMatches( true, 0, s4, 0, 5 )
    请查看String.equals()方法的实现代码,注意学习其实现方法.

                                                                                                                                           equals的源代码

                                                                                                       

    public class StringEquals {
       
    /**
         * @param args the command line arguments
         */
       
           public static void main(String[] args) {
           
                 String s1=new String("Hello");     
                 String s2=new String("Hello");
           
                 System.out.println(s1==s2);//false
                 System.out.println(s1.equals(s2));//true
           
                 String s3="Hello";    
                 String s4="Hello";
             
                 System.out.println(s3==s4); //true      
                 System.out.println(s3.equals(s4));//true
       
           }
    }

    结果原因:s1.equals(s2)比较两个字符串的内容是否相等。使用==比较两字符串变量是否引用同一子串对象。s1,s2用new创建为不同对象,s3,s4引用同一个字符串对象。

     四、字符串查找

    查询字串是否以某字串开头和结尾:startsWith和endWith方法 。
    string.startsWith("")字符串首查找,string.startswith("",i)表示从第i个字母当首查找,同理string.endsWith("")字符串尾查找。
    在字串查找字符或子串,调用indexOf和lastIndexOf方法即可。
    letter.indexOf('')查找最先下一个字符的位置,letter.indexOf('',i)从i开始查找下一个字符的位置,letter.lastIndexOf('')查找最后出现这个字符的位置,letter。lastIndexOf('',i)从i开始查找最后出现这个字符的位置。
    五、字符串中的哈希定位

    哈希码产生的依据:哈希码并不是完全唯一的,它是一种算法,让同一个类的对象按照自己不同的特征尽量的有不同的哈希码,但不表示不同的对象哈希码完全不同。也有相同的情况,看程序员如何写哈希码的算法。

      下面给出几个常用的哈希码的算法。

      1:Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样。

      2:String类的hashCode.根据String类包含的字符串的内容,根据一种特殊算法返回哈希码,只要字符串内容相同,返回的哈希码也相同。

      3:Integer类,返回的哈希码就是Integer对象里所包含的那个整数的数值,例如Integer i1=new Integer(100),i1.hashCode的值就是100 。由此可见,2个一样大小的Integer对象,返回的哈希码也一样。

    String类已内置了生成Hash码的方法。string.hashCode()不同字符串对应不同数值
     
    六、字符串常用功能
    1、提取子串,调用subString方法
    letter.substring(a)指提取a到最后,若substring(a,b)指提取a到b
     
    2、字串连接,调用concat方法或直接使用“+”运算符。
    string.concat(s1)将string和s1连接在一起,并且不改变string的字符串。
     
    3将其它类型的数据转为字串,调用静态方法String.valueOf()。
    String.valueOf(a) 中a可为其他数据类型, char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; String.valueOf( charArray, 3, 2 )表示charArray从第3个开始的两个转化为String类型。
     
    4、Length():获取字串长度
     

     

    charAt():获取指定位置的字符
    getChars():获取从指定位置起的子串复制到字符数组中(它有四个参数,在示例中有介绍)
    四个参数的含义
          1.被拷贝字符在字串中的起始位置
          2.被拷贝的最后一个字符在字串中的下标再加1
          3.目标字符数组
          4.拷贝的字符放在字符数组中的起始下标
    char charArray[];
     
          s1 = new String( "hello there" );
          charArray = new char[ 5 ];
          s1.getChars( 0, 5, charArray, 0 );
    replace():子串替换

     s.replace('a','b')将a中‘a’换为‘b’,或者将是s.replace(s,"aasd")将s全部替换为后者,不过s在后面使用时仍不变。

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

    trim():去除头尾空格

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

    charAt(i)输出第i个字符

    getChars( 0, buf.length(), charArray, 0 ); //(起始下标,长度,字符数组名,起始下标)将buf中的内容赋给charArray[]。

     七、

    String类的方法可以连续调用:String str="abc";String result=str.trim().toUpperCase().concat("defg");

    请阅读JDK中String类上述方法的源码,模仿其编程方式,编写一个MyCounter类,它的方法也支持上述的“级联”调用特性,

    其调用示例为:MyCounter counter1=new MyCounter(1);MyCounter counter2=counter1.increase(100).decrease(2).increase(3);

     1 public class MyCounter {
     2         public int counter;
     3         MyCounter(int i)
     4         {
     5                 counter=i;
     6         }
     7         public  MyCounter increase(int add)
     8         {
     9                 counter=counter+add;
    10                 return this;
    11         }
    12         public MyCounter decrease(int subtraction)
    13         {
    14                 counter=counter-subtraction;
    15                 return this;
    16         }
    17 static public void main(String args[])
    18 {
    19         MyCounter counter1=new MyCounter(1);
    20         MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
    21     System.out.println("counter1.increase(100).decrease(2).increase(3):"+counter2.counter);
    22         }
    23 }
    执行结果:

  • 相关阅读:
    李白—烂尾楼题记
    [原创]网络图片延迟加载实现,超越jquery2010年3月26日
    利用反射,泛型,扩展方法快速获取表单值到实体类
    断点续传 到底是很么
    认识LINQ
    Gridview控件用法大总结
    网站性能优化总结。
    JQ小技巧
    自己写的jq_3个小插件
    MOSS中SPuser类的使用
  • 原文地址:https://www.cnblogs.com/watm/p/7735789.html
Copyright © 2011-2022 走看看