您的位置:首页 > 编程语言 > Java开发

java字符串

2015-10-21 15:33 525 查看
Java.equas()方法的源代码

public boolean equals(Object anObject){

if(this==anObject){

return true;

}

If(anObject instanceof String){

String anotherString=(String)anObject;

int n=count;

if (n==anotherString.count){

char V1[]=value;

charV2[]=anotherString.value;

int i=offset;

int j=anotherString.offset;

while(n--!=0){

if(V1[i++]!=V2[j++])

return false;

}

return true;

}

}

return false;

}

Sring类的使用说明

int Length():返回当前字符段长度

char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。

例如:String s = new
String("abcdefghijklmnopqrstuvwxyz");

System.out.println("s.charAt(5): " + s.charAt(5) );

结果为: s.charAt(5): f

1. void
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。

例如:char[] s1 = {'I','
','l','o','v','e',' ','h','e','r','!'};

String s2 = new String("you!");

s2.getChars(0,3,s1,7);

System.out.println( s1 );

结果为:I love you!

String
replace(char
oldChar, charnewChar)将字符号串中第一个oldChar替换成newChar

String toLowerCase() :将字符串转换成小写。

String toUpperCase() :将字符串转换成大写。

例如:String s = new String("java.lang.Class String");

System.out.println("s.toUpperCase(): " +
s.toUpperCase() );

System.out.println("s.toLowerCase(): " +
s.toLowerCase() );

结果为:s.toUpperCase():
JAVA.LANG.CLASS STRING

s.toLowerCase(): java.lang.class string

public String
trim()
返回该字符串去掉开头和结尾空格后的字符串

凯撒密码:

设计思想:首先获取要加密的内容以及密钥,凯撒密码的密钥即字符移动的位数。由

于凯撒密码器的移位是针对字符的,因此需要将待加密的内容中每个字符取出,读取要加密的字符串。取出字符串中的每个字符每个字符串进行移位

然后针对每个字符分别加以移位

程序流程图:



import java.util.Scanner;

public class Number {

private String table; // 定义密钥字母表

private int key; // 定义密钥key

public Number(String table, int key) {

// 根据不同的字母表和不同的密钥生成一个新的凯撒算法,达到通用的目的

super();

this.table = table;

this.key = key;

}

public String encrypt(String from) {

//凯撒加密算法,传入明文字符串,返回一个密文字符串

String to = "";

for (int i = 0; i < from.length(); i++) {

to += table.charAt((table.indexOf(from.charAt(i))+key)%table.length());

}

return to;

}

public static void main(String[] args) {

Number number = new Number("abcdefghijklmnopqrstuvwxyz", 3);

Scanner scanner = new Scanner(System.in);

System.out.println("请输入要加密的字符串");

String str =scanner.nextLine(); //输入字符串 security

String result = number.encrypt(str); //调用加密方法进行加密

System.out.print(result); // 可得结果 vhfxulwb

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: