• 归档:字符串类


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

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

    (1)程序设计思想:

        读入一个字符串,

        加密:遍历该字符串,如果该字符串原先为 x , y , z则减去23,否则加上3

        解密:遍历该字符串,如果该字符串原先为 a , b , c则加上23,否则减去3

    (2)程序流程图:

    加密:

    解密:

    (3)源代码:

     1 package keTang07;
     2 
     3 import java.util.Scanner;
     4 
     5 public class JiaJieMi {
     6 
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9         Scanner scan = new Scanner(System.in);
    10         String str = scan.next();
    11         
    12         String jm = jiaMi(str);
    13         System.out.println("将 " + str + " 加密结果 " + jm);
    14         
    15         jm = jieMi(str);
    16         System.out.println("将 " + str + " 解密结果 " + jm);
    17         scan.close();
    18     }
    19     
    20     public static String jiaMi(String str){
    21         StringBuffer jm = new StringBuffer();
    22         char ch;
    23         for(int i = 0;i < str.length();i++){
    24             if(  (str.charAt(i) >= 'x' && str.charAt(i) <= 'z')   ||   (str.charAt(i) >= 'X' && str.charAt(i) <= 'Z')  ){
    25                 ch = (char) (str.charAt(i) - 23);
    26                 jm.append(ch);
    27             }
    28             else{
    29                 ch = (char) (str.charAt(i) + 3);
    30                 jm.append(ch);
    31             }
    32         }
    33         
    34         return jm.toString();
    35     }
    36     
    37     public static String jieMi(String str){
    38         StringBuffer jm = new StringBuffer();
    39         char ch;
    40         for(int i = 0;i < str.length();i++){
    41             if(  (str.charAt(i) >= 'a' && str.charAt(i) <= 'c')   ||   (str.charAt(i) >= 'A' && str.charAt(i) <= 'C')  ){
    42                 ch = (char) (str.charAt(i) + 23);
    43                 jm.append(ch);
    44             }
    45             else{
    46                 ch = (char) (str.charAt(i) - 3);
    47 
    48                 jm.append(ch);
    49             }
    50         }
    51         
    52         return jm.toString();
    53     }
    54 }

    (4)结果截图:

     

     

     

    2、判断字符串相等

    "=="判断引用的地址是否相等,equals()方法比较的是值是否相等

    由相同的字符组成的两个字符串常量属于同一对象,位于内存中的同一位置,所以s0,s1均指向"Hello"的地址,所以 s0 == s1 为 true

    s2 = "He" + "llo"在编译时直接执行,故s2也指向"Hello"的地址,所以 s0 == s2 也为 true,

    new String()为开辟字符串对象,每次实例化的对象在内存中都为不同的对象,所以地址也不一样

    所以 new String("Hello")  ==  new String("Hello")  为 false

     

    s1 指向内存中的字符串常量"a", s2 = s1; 说明s2也指向内存中的字符串常量"a",所以s1 == s2 为true

    因为字符串为不可变量,使用 '+' , 相当于得到了一个新的字符串对象,所以 s1 += "b" s1指向新的字符串常量"ab",所以 s1 == s2 为false

    s1 == "ab" 中的"ab"是一个常量,与s1所引用的"ab"不是一个东西,所以为false

    equals()方法判断的是值是否相等,所以 s1.equals("ab") 比较的是s1引用的值是不是"ab",所以为true

     

    3、String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明、阅读笔记

    (1)

    length

    public int length()
    Returns the length of this string. The length is equal to the number of Unicode code units in the string.
    Specified by:
    length in interface CharSequence
    Returns:
    the length of the sequence of characters represented by this object.
    返回此字符串的长度。
    例如:

    String str = "Hello";
    System.out.println(str.length()); //5

    (2)

    charAt

    public char charAt(int index)
    Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

    If the char value specified by the index is a surrogate, the surrogate value is returned.

    Specified by:
    charAt in interface CharSequence
    Parameters:
    index - the index of the char value.
    Returns:
    the char value at the specified index of this string. The first char value is at index 0.
    Throws:
    IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.
    返回指定索引处的 char 值。
    例如:

    String str = "Hello";
    System.out.println(str.charAt(2));//  l

    (3)

    getChars

    public void getChars(int srcBegin,
                         int srcEnd,
                         char[] dst,
                         int dstBegin)
    Copies characters from this string into the destination character array.

    The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied issrcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

         dstBegin + (srcEnd-srcBegin) - 1
     
    Parameters:
    srcBegin - index of the first character in the string to copy.
    srcEnd - index after the last character in the string to copy.
    dst - the destination array.
    dstBegin - the start offset in the destination array.
    Throws:
    IndexOutOfBoundsException - If any of the following is true:
    • srcBegin is negative.
    • srcBegin is greater than srcEnd
    • srcEnd is greater than the length of this string
    • dstBegin is negative
    • dstBegin+(srcEnd-srcBegin) is larger than dst.length

     将字符从此字符串复制到目标字符数组。

    (4)

    replace

    public String replace(char oldChar,
                          char newChar)
    Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

    If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a String object is returned that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

    Examples:

     "mesquite in your cellar".replace('e', 'o')
             returns "mosquito in your collar"
     "the war of baronets".replace('r', 'y')
             returns "the way of bayonets"
     "sparring with a purple porpoise".replace('p', 't')
             returns "starring with a turtle tortoise"
     "JonL".replace('q', 'x') returns "JonL" (no change)
     
    Parameters:
    oldChar - the old character.
    newChar - the new character.
    Returns:
    a string derived from this string by replacing every occurrence of oldChar with newChar.
    返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

    (5)

    toUpperCase

    public String toUpperCase()
    Converts all of the characters in this String to upper case using the rules of the default locale. This method is equivalent to toUpperCase(Locale.getDefault()).

    Note: This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags. For instance, "title".toUpperCase() in a Turkish locale returns "Tu0130TLE", where 'u0130' is the LATIN CAPITAL LETTER I WITH DOT ABOVE character. To obtain correct results for locale insensitive strings, use toUpperCase(Locale.ROOT).

    Returns:
    the String, converted to uppercase.
    See Also:
    toUpperCase(Locale)
     使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

     

    (6)

    toLowerCase

    public String toLowerCase()
    Converts all of the characters in this String to lower case using the rules of the default locale. This is equivalent to calling toLowerCase(Locale.getDefault()).

    Note: This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags. For instance, "TITLE".toLowerCase() in a Turkish locale returns "tu0131tle", where 'u0131' is the LATIN SMALL LETTER DOTLESS I character. To obtain correct results for locale insensitive strings, use toLowerCase(Locale.ROOT).

    Returns:
    the String, converted to lowercase.
    See Also:
    toLowerCase(Locale)
     使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

    (7)

    trim

    public String trim()
    Returns a string whose value is this string, with any leading and trailing whitespace removed.

    If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than 'u0020' (the space character), then a reference to this String object is returned.

    Otherwise, if there is no character with a code greater than 'u0020' in the string, then a String object representing an empty string is returned.

    Otherwise, let k be the index of the first character in the string whose code is greater than 'u0020', and let m be the index of the last character in the string whose code is greater than 'u0020'. A String object is returned, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m + 1).

    This method may be used to trim whitespace (as defined above) from the beginning and end of a string.

    Returns:
    A string whose value is this string, with any leading and trailing white space removed, or this string if it has no leading or trailing white space.
    返回字符串的副本,忽略前导空白和尾部空白。

    (8)

    toCharArray

    public char[] toCharArray()
    Converts this string to a new character array.
    Returns:
    a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
     将此字符串转换为一个新的字符数组。

     

  • 相关阅读:
    CF 676C. Vasya and String 尺取经典题目
    进制转换
    《Dotnet9》系列-开源C# Winform控件库1《HZHControls》强力推荐
    《Dotnet9》系列-开源C# Winform控件库强力推荐
    《Dotnet9》系列-开源C# WPF控件库强力推荐
    《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐
    《Dotnet9》系列-开源C# WPF控件库2《Panuon.UI.Silver》强力推荐
    《Dotnet9》系列之建站-Dotnet9建站20天感悟
    《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐
    《Dotnet9》系列-FluentValidation在C# WPF中的应用
  • 原文地址:https://www.cnblogs.com/liuxining/p/6001092.html
Copyright © 2020-2023  润新知