java常用实用类
Posted 疏桐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java常用实用类相关的知识,希望对你有一定的参考价值。
1、String类概念
java程序中默认导入java.lang包的,像java.lang.String等String类属于final类,用户不能扩展String类,String 类没有子类。String类常见的方法有如下:
public int length(); public boolean equals(); public boolean startsWith(); public bolean endsWith() ; public int compareTo(); package com.Example1; public boolean contains(); public int indexOf() ; public String substring(int startpoint); public String trim(); public staticparseInt(); public static String valueOf();
public void getChars(int start,int end,char[],int offset); public char[] to CharArray(); public Boolean matches(String regex);
实例:
在for循环输出中使用了 for(string i: a) {} 遍历输出 这里确定i De 类型主要看 之前申明的时候 a 的类型
import java.util.Arrays;
public class Example_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] a = { "mellon", "apple", "pear" };
String[] b = { "xigau", "apple", "xihuonshi" };
System.out.println("使用字典顺序排列数组中值");
System.out.println("****************");
SortString.sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(" " + a[i]);
}
System.out.println("***************");
System.out.println("使用类库中的Arrays排序字典");
Arrays.sort(b);
for (String i : b) {
System.out.println(i);
}
}
}
class SortString {
public static void sort(String a[]) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[i]) < 0) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
toString 的实例重写
package com.Example1;
import java.util.Date;
public class Example8_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date = new Date();
System.out.println(date.toString());
TV2 TV2 = new TV2("长虹电视");
System.out.println(TV2.toString());
}
}
class TV2 {
String name;
public TV2() {
}
public TV2(String s) {
this.name = s;
}
public String toString() {
String oldStr = super.toString();
return oldStr + "\\n 这是电视机, 品牌是: " + name;
}
}
使用String类进行加密和解密
package com.Example1;
import java.util.Scanner;
public class Example8_8 {
public static void main(String[] args) {
String souceString = "宁宁 我爱你";
EncryptAndDecrypt person = new EncryptAndDecrypt();
System.out.println("输入加密密码来加密密文" + souceString);
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
String secret = person.encrypt(souceString, password);
System.out.println("密文" + secret);
System.out.println("输入解密密码");
password = scanner.nextLine();
String source = person.decrypt(souceString, password);
System.out.println("明文" + source);
}
}
class EncryptAndDecrypt {
String encrypt(String souceString, String password) {
char[] p = password.toCharArray();
int n = p.length;
char[] c = souceString.toCharArray();
int m = c.length;
for (int k = 0; k < m; k++) {
int mima = c[k] + p[k % n];
c[k] = (char) mima;
}
return new String(c);
}
String decrypt(String souceString, String password) {
char[] p = password.toCharArray();
int n = p.length;
char[] c = souceString.toCharArray();
int m = c.length;
for (int k = 0; k < m; k++) {
int mima = c[k] - p[k % n];
c[k] = (char) mima;
}
return new String(c);
}
}
2、正则表达式和String类的替换概念
原地址: https://blog.csdn.net/qq_30057549/article/details/76735996
以上是关于java常用实用类的主要内容,如果未能解决你的问题,请参考以下文章