提取字符串
char ch;
ch = “orange”.charAt(3);
它将从index(3)中提取单个字符串“n”并将其存储在变量ch中。
如何使用字符串提取或字符提取,使用String类的方法,如subString()、concat()、replace()和trim()。
public class StringMethods {
/** 构造方法 */
protected StringMethods() {
}
/**
* 这是 main 方法
* @param args
* 传递至 main 方法的参数
*/
public static void main(String[] args) {
String s = "Java is a " + "platform independent language";
String s1 = "Hello world";
String s2 = "Hello";
String s3 = "HELLO";
System.out.println(s);
System.out.println("index of t = " + s.indexOf('t'));
System.out.println("last index of t = " + s.lastIndexOf('t'));
System.out.println("index of(t, 10) = " + s.indexOf('t', 10));
System.out.println(s1.substring(3, 8));
System.out.println(s2.concat("World"));
System.out.println(s2.replace('l', 'w'));
System.out.println(s1.trim());
}
}
更改字符串中字符的大小写
小写->大写,使用toUpperCase( )方法
语法:public String toUpperCase();
大写->小写,使用 toLowerCase( )方法
语法:Public String toLowerCase();
更改字符串中字符的大小写,使用String类的方法,如toUpperCase()和toLowerCase()。
public class StringTest {
/** 构造方法 */
protected StringTest() {
}
/**
* 这是 main 方法 它演示字符串的 length() 和 UpperCase() 方法
* @param args
* 传递至 main 方法
*/
public static void main(String[] args) {
String name = new String("George");
System.out.println("姓名是" + name);
int length = name.length();
System.out.println("姓名的长度为 ” + length + “ 个字符");
System.out.println("姓名用大写形式表示为: ");
String nameUppercase = name.toUpperCase();
System.out.println(nameUppercase);
}
}
StringBuffer类
StringBuffer 用于表示可以修改的字符串
使用连接运算符 (+) 的字符串会自动创建字符串缓冲对象
不变性
StringBuffer类:String 的对等类、表示可增加和可编写字符的可变序列、将字符插入到字符串中间或附加到字符串末尾
StringBuffer类的用法、使用StringBuffer类的方法,如append()、insert()、replace()、setChar()和toString()。
public class StringBuf {
/** 构造方法 */
protected StringBuf() {
}
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Java");
buf.append(" Guide Ver1/");
buf.append(3);
int index = 5;
buf.insert(index, "Student ");
index = 23;
buf.setCharAt(index, '.');
int start = 24;
int end = 25;
buf.replace(start, end, "4");
String s = buf.toString(); // 转换为字符串
System.out.println(s);
}
}
Math类
Math类的用法、使用Math类的方法,如ceil()、floor()和round()。
public class MathDemo {
/** 构造方法 */
protected MathDemo() {
}
/**
* main 方法演示 Math 类的不同方法
* @param args
* 传递至 main 方法的参数
*/
public static void main(String[] args) {
/** 此变量存储 num 的值 */
int num = 38;
/** 该变量存储 num1 的值 */
float num1 = 65.7f;
System.out.println(Math.ceil(num));
System.out.println(Math.ceil(num1));
System.out.println(Math.floor(num));
System.out.println(Math.floor(num1));
System.out.println(Math.round(num));
System.out.println(Math.round(num1));
}
}
Class类
自动创建对象无需声明,通过使用对象中的 getClass( ) 方法,或使用静态 forName( ) 方法,或使用自定义 ClassLoader对象加载新类。
Class类的用法、使用Class类的方法,如getClass()和getSupperClass()。
public class StoreString {
/** 构造方法. */
protected StoreString() {
}
private String name = "diana";
}
/** 这个类扩展 StoreString 类. */
public class StoreInteger {
/** 构造方法. */
protected StoreInteger() {
}
/** 该变量存储整数值. */
private int deptno;
}
public class ClassDemo {
/** 构造方法 */
protected ClassDemo() {
}
/**
* 这个类演示 Class 类的访问方法
* @param args
* 传递至 main 方法的参数
*/
public static void main(String[] args) {
StoreString objString = new StoreString();
StoreInteger objInteger = new StoreInteger();
Class objClass;
objClass = objString.getClass();
System.out.println("objString 对象的类型是: " + objClass.getName());
objClass = objInteger.getClass();
System.out.println("objInteger 对象的类型是: " + objClass.getName());
objClass = objClass.getSuperclass();
System.out.println("objInteger的父类是" + objClass.getName());
}
}
Object类
Object类的用法、使用Object类的方法,如equals()。
/**
* 这个类演示 Object 类.
* @version 1.0, 2005 年 6 月 13 日 * @author Ben
*/
public class ObjectDemo {
/** 构造方法 */
protected ObjectDemo() {
}
/**
* 这是 main 方法
* 它演示 Object 类
* @param args
* 传递至 main 方法的参数
*/
public static void main(String[] args) {
if (args[0].equals("Java")) {
System.out.println("是, Java 是一项非常好的技术!");
}
}
}