java提取字符串字符_Java中的字符提取

java提取字符串字符

There are several ways by which characters can be extracted from String class object. String is treated as an object in Java so we can’t directly access the characters that comprise a string. For doing this String class provides various predefined methods.

有几种方法可以从String类对象中提取字符。 字符串在Java中被视为对象,因此我们无法直接访问组成字符串的字符。 为此,String类提供了各种预定义方法。

Java中的字符提取 (Character Extraction in Java)

charAt() (charAt())

charAt() method is used to extract a single character at an index. It has following syntax.

charAt()方法用于在索引处提取单个字符。 它具有以下语法。

Syntax

句法

char charAt(int index)

Example

class temp
{
	public static void main(String...s)
	{
		String str="Hello";
		char ch=str.charAt(2);
		System.out.println(ch);
	}	
}

Output l

输出 l

In above example ch will contain character l. We must take care that the index should not be negative and should not exceed string length.

在上面的示例中,ch将包含字符l。 我们必须注意索引不应为负,并且不应超过字符串长度。

getChars() (getChars())

It is used to extract more than one character. getChars() has following syntax.

它用于提取多个字符。 getChars()具有以下语法。

Syntax

句法

void getChars(int stringStart, int stringEnd, char arr[], int arrStart)

Here stringStart and stringEnd is the starting and ending index of the substring. arr is the character array that will contain the substring. It will contain the characters starting from stringStart to stringEnd-1. arrStart is the index inside arr at which substring will be copied. The arr array should be large enough to store the substring.

这里的stringStart和stringEnd是子字符串的开始和结束索引。 arr是将包含子字符串的字符数组。 它将包含从stringStart到stringEnd-1的字符。 arrStart是arr内部的索引,将在该索引处复制子字符串。 arr数组应足够大以存储子字符串。

Example

class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=new char[4];
str.getChars(1,5,ch,0);
System.out.println(ch);
} 
}

Output ello

输出 ello

getBytes() (getBytes())

getBytes() extract characters from String object and then convert the characters in a byte array. It has following syntax.

getBytes()从String对象提取字符,然后将其转换为字节数组。 它具有以下语法。

Syntax

句法

byte [] getBytes()

Example

String str="Hello";
byte b[]=str.getBytes();

toCharArray() (toCharArray())

It is an alternative of getChars() method. toCharArray() convert all the characters in a String object into an array of characters. It is the best and easiest way to convert string to character array. It has following syntax.

它是getChars()方法的替代方法。 toCharArray()将String对象中的所有字符转换为字符数组。 这是将字符串转换为字符数组的最佳和最简单的方法。 它具有以下语法。

Syntax

句法

char [] toCharArray()

Example

class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=str.toCharArray();
System.out.println(ch);
} 
}

Output Hello World

输出 Hello World

If you found anything wrong or have any doubts regarding above tutorial then comment below.

如果您发现任何错误或对以上教程有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2015/07/character-extraction-in-java.html

java提取字符串字符

  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值