Java.lang.Character类中charCount(int codePoint)方法具有什么功能呢?

转自:

Java.lang.Character类中charCount(int codePoint)方法具有什么功能呢?

下文笔者将讲述Character类中charCount(int codePoint)方法的功能,如下所示:

Java.lang.CHaracter之charCount(int codePoint)方法的功能

charCount(int codePoint)方法的功能:
用于表示指定字符所需的字符的值的数目(Unicode代码点)
当指定的字符是等于或大于0x10000,则此方法返回2,否则此方法返回1

注意事项:
    此方法不是验证指定的字是否有效的Unicode代码点

charCount(int codePoint)语法说明

 public static int charCount(int codePoint)
 
  参数说明
   codePoint:字符(Unicode代码点)进行测试

  返回值 
    当字符是一个有效的补充字符,方法将返回2
    否则返回1

例:

package com.java.other;

import org.junit.Test;

public class other {

	/**
	 * java265.com 测试示例分享
	 */
	@Test
	public void test() {
		/**
		 * Character类 charCount(int codePoint)方法的示例
		 */
		int c = 0x888;
		int r = Character.charCount(c);
		System.out.println(r);
	}
}
-------运行以上代码,将输出以下信息---
1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
统计单词个数可以这样实现: ```java import java.util.Scanner; import java.io.*; public class WordCount { public static void main(String[] args) throws IOException { File file = new File("test.txt"); Scanner scanner = new Scanner(file); int count = 0; while (scanner.hasNext()) { count++; scanner.next(); } System.out.println("单词数:" + count); scanner.close(); } } ``` 这个程序首先打开一个名为 `test.txt` 的文件,然后用 `Scanner` 对象扫描文件中的每个单词,计算单词数。 统计文件中的字符数、单词数和行数可以这样实现: ```java import java.util.Scanner; import java.io.*; public class FileCount { public static void main(String[] args) throws IOException { File file = new File("test.txt"); Scanner scanner = new Scanner(file); int charCount = 0; int wordCount = 0; int lineCount = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); lineCount++; charCount += line.length(); wordCount += line.split("\\s+").length; } System.out.println("字符数:" + charCount); System.out.println("单词数:" + wordCount); System.out.println("行数:" + lineCount); scanner.close(); } } ``` 这个程序首先打开一个名为 `test.txt` 的文件,然后用 `Scanner` 对象扫描文件中的每一行,计算字符数、单词数和行数。其中,字符数等于每一行的字符数之和,单词数等于每一行的单词数之和,行数等于文件中的行数。 需要注意的是,在计算单词数时,不能简单地使用空格分隔符,因为单词之间可能存在其他分隔符,如逗号、句号等。因此,可以使用正则表达式 `\\s+` 来匹配所有的空白符,包括空格、制表符、换行符等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值