JAVA NOTEBOOK (week 2)

本文介绍Java中字符串操作的基础知识,包括String类的构造方法、常用方法如length(), equals(), substring()等,以及StringTokenizer类的基本用法。通过具体示例展示了如何使用这些工具进行字符串解析和分词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

KEY POINTS:

1. packages 

2.import

3.API:https://docs.oracle.com/javase/1.5.0/docs/api/    //where to find the usage of packages and methods


1) about import

If you use only a few classes from a package, it is good style to import them individually. This makes it easier to see which classes your program actually uses.
import java.util.Vector;

If you use a lot of classes from a package, it probably makes more sense to use a wild card to import them all.

import java.util.*;

2) 

2.1The  java.lang.String  Class

String(). Constructs a new String object that represents an empty character sequence.
examples:
String(char[] value). Constructs a new String object that represents the sequence ofcharacters contained in the character array.
examples:
String(String original). Constructs a new String object that represents the same sequence ofcharacters as the argument.
examples:
int length(). Obtains the number of characters in the String.
examples: str.length() 
char charAt(intindex). Returns the character at the specified index.
examples: str.charAt(idx_end)
boolean equals(ObjectanObject).Returns true if the specified Object represents a String with the same sequenceof characters.
int  indexOf(int ch).Returns the index of the first occurrence of the character.
int  indexOf(String str).Returns the index of the first occurrence of the String.
boolean  startsWith(String prefix). Checks if the String has the specified prefix.
String  substring(int beginIndex,int endIndex).Returns a substring. 
–boolean equals() 
examples: x.equals(y)  //x和y引用的是同一类对象且属性内容相等时,返回True
–boolean equalsIgnoreCase()
examples: x.equalsIgnoreCase(y)

-substring

examples:data.substring(idx_start, idx_end)

-indexof

examples: data.indexOf("_", idx_start)

2.2 The  java.util.StringTokenizer  Class 

StringTokenizer(String str).Constructs a string tokenizer.The tokenizeruses the default delimiter set, white space.
StringTokenizer(String str,String delim).Constructs a string tokenizer.The argument delimcontains the character delimiters for separating tokens.
examples: StringTokenizer s1= new StringTokenizer(str,"_")
boolean hasMoreTokens().Tests if there are more tokens to extract.
examples: 
String nextToken(Stringdelim).Returns the next token in the string.
examples: str.nextToken() 
int countTokens().Obtains the number of tokens left to be extracted, not the number of tokens inthe string. 
int num = str.countTokens()

3) Practice

3.1

import java.util.*;

public class Lab2_1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String data = new String("Mini Discs 74 Minutes (10-pack)_5_9.0");
		String name = null;
		String quantity = null;
		String price = null;
		int numOfTokens = 3;
		int k = 0;
		int idx_start = 0;
		int idx_end = 1;
		while(idx_end < data.length()) {
			if(data.charAt(idx_end)=='_' || idx_end==(data.length()-1)) {
				switch (k) { 
				case 0:
					name = data.substring(idx_start, idx_end);
					k++;
					break;
				case 1:
					quantity = data.substring(idx_start, idx_end);
					k++;
					break;
				case 2:
					price = data.substring(idx_start, idx_end+1);
					k++;
					break;
				}
				
				idx_start = idx_end + 1;
			}
			
			idx_end++;
		}
		
		System.out.println(name);
		System.out.println(quantity);
		System.out.println(price);

3.2

package examples;

import java.util.*;

public class Lab1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		StringTokenizer st = new StringTokenizer("This string has five tokens.");
		int num = st.countTokens();		
		
		while(st.hasMoreTokens()) {
			System.out.print(st.nextToken() + ", ");
		}
		System.out.println();
		
		for(int i=0;i<num;i++) {
			System.out.print(st.nextToken() + ", ");
		}
        	System.out.println();
		
		// TODO Auto-generated method stub
		String s1 = new String("hello");
		String s2 = new String("Hello"); 
		String s3 = new String("hello");
		
		System.out.println(s1 + s1.length() + s2 + s2.length() + s3 + s3.length());		
                System.out.println(1+2+3);
		System.out.println(""+1+2+3);
		
		System.out.println("s1==s2 : " + (s1==s2));
		System.out.println("s1==s3 : " + (s1==s3));
		System.out.println("s2==s3 : " + (s2==s3));	
		
		System.out.println("s1.equals(s2) : " + s1.equals(s2));
		System.out.println("s1.equals(s3) : " + s1.equals(s3));
		System.out.println("s2.equals(s3) : " + s2.equals(s3));
		System.out.println("s1.equalsIgnoreCase(s2) : " + s1.equalsIgnoreCase(s2));
		System.out.println("s1.equalsIgnoreCase(s3) : " + s1.equalsIgnoreCase(s3));
		System.out.println("s2.equalsIgnoreCase(s3) : " + s2.equalsIgnoreCase(s3));
	}

}

3.3

package examples;

public class Lab2_2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String data = new String("Mini Discs 74 Minutes (10-pack)_5_9.0");
		int idx_start = 0;
		int idx_end = 0;
		
		idx_end = data.indexOf("_", idx_start);
		String name = data.substring(idx_start, idx_end);
		idx_start = idx_end + 1;
		idx_end = data.indexOf("_", idx_start);
		String quantity = data.substring(idx_start, idx_end);
		idx_start = idx_end + 1;
		idx_end = data.indexOf("_", idx_start);
		String price = data.substring(idx_start, data.length());
		
		System.out.println(name);
		System.out.println(quantity);
		System.out.println(price);
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值