首页 > Java > java教程 > 正文

Java中的字符串类

WBOY
发布: 2024-08-30 15:42:54
原创
252 人浏览过

字符串是Java中扩展java.lang的最终类。表示为字符串的对象。 Serialized、Comparable 和 CharSequence 接口由 string 类实现。所有字符串文字(例如“string example”)都被视为 String 类的实例。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

字符串的工作

  • 如果你想要这样的字符串:我在孟买的“宫殿”。
  • 在这种情况下,java编译器会误解并抛出错误“我在孟买的“宫殿”。”
  • 会给编译器带来不明确的情况。
  • 为了处理这种情况,反斜杠“”字符将派上用场。

代码:

public class test
{
public static void main(String[] args)
{
String s1 = "I am at a \"Palace \" of Mumbai.";
System.out.println(s1);
}
}
登录后复制

输出:

Java中的字符串类

示例#1:添加两个字符串。

如果将两个字符串相加,结果将是字符串连接。

代码:

public class test
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
int c = a+b;
System.out.println(c);
String s = "10";
int d = 10;
String s1 = s+d;
System.out.println(s1);
}
}
登录后复制

输出:

Java中的字符串类

示例#2:字符串是不可变的。

字符串一旦创建,其值就无法更改。字符串是恒定的。这样的属性被称为不可变的。

代码:

class Test
{
public static void main(String args[])
{
String str="I am string";
str.concat(" I am instance of String class.");
System.out.println(str);
}
}
登录后复制

输出:

Java中的字符串类

说明:

  • 首先,您创建了字符串 str 作为“I am string”。然后借助 string 的 concat 方法连接一些字符串。
  • 但是由于字符串是不可变的,因此它们的值或状态无法更改。连接的字符串将占用另一部分内存。
  • 引用 str 仍然指“我是字符串”。
  • 要更新同一字符串的值,我们必须将连接的字符串分配给特定引用,如下例所示。

示例 #3

代码:

class Test
{
public static void main(String args[])
{
String str="I am string";
str = str.concat(" I am instance of String class.");
System.out.println(str);
}
}
登录后复制

输出:

Java中的字符串类

要创建可变字符串,可以使用 StringBuffer 或 StringBuilder 类。

字符串类的类型

Java中有两种类型的字符序列类。不可变类是 String 类,可变类是 StringBuilder 和 StringBuffer。

StringBuilder 和 StringBuffer 有一些区别。

  • StringBuffer 是线程安全且同步的。这意味着没有两个线程可以同时访问字符串缓冲区的方法。
  • StringBuilder 是非同步的并且不是线程安全的。这意味着两个线程可以同时访问字符串缓冲区的方法。
  • 说到效率,StringBuilder 比 StringBuffer 更高效。

下面给出的是 StringBuffer 和 StringBuilder 的示例

1.字符串缓冲区

代码:

class StringBufferExample
{
public static void main(String args[])
{
StringBuffer bufferstring=new StringBuffer("I am stringbuffer.");
bufferstring.append("I am thread safe.");
System.out.println(bufferstring);
}
}
登录后复制

输出:

Java中的字符串类

2.字符串生成器

代码:

class StringBuilderExample
{
public static void main(String args[])
{
StringBuilder builderstring=new StringBuilder("I am stringbuilder.");
builderstring.append("I am more efficient than string buffer.");
System.out.println(builderstring);
}
登录后复制

输出:

Java中的字符串类

String 类在 Java 中如何工作?

在Java中,字符串是字符的序列,与c、c++不同,它们是String类的对象。

该类支持一些构造函数,如下:

Java 中重要的字符串构造函数

下面给出了java中一些重要的字符串构造函数:

  • String(): 它用空数据初始化一个字符串对象。
  • String(byte[] bytes): 它通过解码字节数组并将其分配给创建的引用来构造字符串。
  • String(char[] value): 它通过提供的字符数组构造一个字符串,并将其分配给创建的引用。

Java 中重要的字符串方法

下面给出了java中一些重要的字符串方法:

  • charAt(int index): It returns the character present at the given index in a string.
  •  concat(String s): It concatenates the existing string with the string provided as an argument.
  • equals(Object anObject): Compares this string to the given object and return boolean.
  • substring(int beginIndex, int endIndex): It returns a string, i.e. substring of this string.
  • toLowerCase(): Converts a string to lower case.
  • toUpperCase(): Converts a string to upper case.
  • startsWith(String prefix): Tests if the string starts with the prefix provided as an argument.
  • trim(): It trims all leading and trailing whitespaces from the string.

Examples of String Class in Java

Given below are the examples of String Class in Java:

Example #1

Let us check whether the string contains only whitespaces.

Code:

class StringWhitespaceChecker
{
public static boolean isStringWithWhitespace(String s)
{
if(s.trim().isEmpty)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
StringWhitespaceChecker s1 = new StringWhitespaceChecker();
String s =new String(" ");
String string = new String(" I am string. ");
boolean condition1 = s1.isStringWithWhitespace(s);
boolean condition2 = s1.isStringWithWhitespace(string);
System.out.println(condition1);
System.out.println(condition2);
}
}
登录后复制

Output:

Java中的字符串类

Explaination:

  • The function first trims all the whitespaces and then checks whether it is empty; if it is so, it will return true or false, respectively.

Example #2

String function.

Code:

class Test
{
public static void main(String args[])
{
String s = new String("hello");
s.toUpperCase();
System.out.println(s);
s = s.concat("world");
System.out.println(s);
System.out.println(s.charAt(1));
String s1 = new String("helllo");
boolean b = s.equals(s1);
System.out.println(b);
}
}
登录后复制

Output:

Java中的字符串类

Example #3

Java String to toCharArray()

Code:

class Test
{
public static void main(String args[])
{
String s = new String("hello");
Char[] c1 = s.toCharArray()
for(int i=0;i<c1.length;i++)
{
System.out.println(c1[i]);
}
}
}
登录后复制

Output:

Java中的字符串类

Conclusion

This article is focused upon the criteria such as introduction to the string, String constructors, String Methods, Types of String classes, String immutable, StringBuffer and StringBuilder. Examples of each property. A string is a very important class in Java. You will work with string manipulation in multiple ways in real-world projects.

以上是Java中的字符串类的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!