知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
      • Theory:What are collections
      • Theory:The collections Framework overview
      • Theory:ArrayList
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
        • Strings and arrays
        • Splitting the string
        • Iterating over a string
        • Conclusion
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-10-06
目录

Theory:Processing strings

As you already know, a string is a sequence of characters with a single data type, and so is an array. Strings are similar to arrays in many ways: in some sense, a string looks like an array of characters. Moreover, you can iterate over both strings and arrays. Sometimes you may need to process a string and convert it into an array, and in this topic we'll learn how to do that.

# Strings and arrays

It's possible to convert between strings and character arrays using special methods like valueOf() and toCharArray().

char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F' };

String stringFromChars = String.valueOf(chars); // "ABCDEF"

char[] charsFromString = stringFromChars.toCharArray(); 
// { 'A', 'B', 'C', 'D', 'E', 'F' }

String theSameString = new String(charsFromString); // "ABCDEF"
1
2
3
4
5
6
7
8

There is another way to turn a string into an array. Take a look:

String text = "Hello";
String[] parts = text.split(""); // {"H", "e", "l", "l", "o"}
1
2

Here we used a much more concise method that splits a string into parts. Let's explore it in more detail!

# Splitting the string

A string can be divided into an array of strings based on a delimiter. To perform this, we call the split method, which divides the string into substrings by a separator. In the previous example, we used the "" delimiter, which automatically splits a string into smaller elements (substrings) that consist of one char.

If the delimiter is specified, the method returns an array of all the substrings. Note that the delimiter itself is not included in any of the substrings:

String sentence = "a long text";
String[] words = sentence.split(" "); // {"a", "long", "text"}
1
2

Let's try to split an American phone number into the country code, the area code, the central office code, and other remaining digits:

String number = "+1-213-345-6789";
String[] parts = number.split("-"); // {"+1", "213", "345", "6789"}
1
2

Keep in mind that all these parts are still strings no matter what they look like!

Choose your delimiter wisely, otherwise you might get some sentences that start with a space:

String text = "That's one small step for a man, one giant leap for mankind.";
String[] parts = text.split(","); 
// {"That's one small step for a man", " one giant leap for mankind."}
1
2
3

You can choose any delimiter you prefer, including a combination of spaces and words:

String text = "I'm gonna be a programmer";
String[] parts = text.split(" gonna be "); // {"I'm", "a programmer"}
1
2

As you can see here, the split method is also a good tool to get rid of something you don't need or don't want to use.

# Iterating over a string

It's possible to iterate over the characters of a string using a loop (while, do-while, for-loop).

See the following example:

String scientistName = "Isaac Newton";

for (int i = 0; i < scientistName.length(); i++) {
    System.out.print(scientistName.charAt(i) + " ");
}
1
2
3
4
5

In strings, like in arrays, indexing begins from 0. In our example, the for-loop iterates over the string "Isaac Newton" . With each iteration, the charAt method returns the current character at the i index, and that character is then printed to the console, followed by a blank space.

Here is what the code outputs as a result:

I s a a c   N e w t o n 
1

# Conclusion

In this topic, you learned about different ways of processing a string. You can convert between a string and an array using the valueOf() and toCharArray() methods or divide a string into substrings with the help of the split() method. You can also iterate over the characters of a string using a loop. All these methods will help you solve different tasks and improve your programming experience.

编辑 (opens new window)
#Java#String
上次更新: 2022/10/12, 17:01:25
Theory:Comparable
Theory:Initialization blocks

← Theory:Comparable Theory:Initialization blocks→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式