Strings


Immutable Strings

  • Objects of the String class are immutable.
  • Every method in the class that appears to modify a String actually creates and returns a brand new String object containing the modification.
  • To the reader of the code, an argument usually looks like a piece of information provided to the method, not something to be modified.

Overloading ‘+’ vs. StringBuilder

  • Because a String is read-only, there’s no possibility that one reference will change something that will affect the other references.
  • There was no mention of StringBuilder in the source code, but the compiler decided to use it anyway, because it is much more efficient.
  • (Using overloading) you’re going to get a new StringBuilder object every time you pass through the loop.
  • If looping is involved, you should explicitly use a StringBuilder in your toString( ).
  • Prior to Java SE5, Java used StringBuffer, which ensured thread safety and so was significantly more expensive.

Unintended recursion

  • If you really do want to print the address of the object, the solution is to call the Object toString( ) method.

Operations on Strings

  • Every String method carefully returns a new String object when it’s necessary to change the contents.
  • If the contents don’t need changing, the method will just return a reference to the original String.

Formatting output

printf()

  • format specifier: in addition to telling where to insert the value, they also tell what kind of variable is to be inserted and how to format it.

System.out.format()

  • Java SE5 introduced the format( ) method, available to PrintStream or PrintWriter objects , which includes System.out.
  • The format( ) method is modeled after C’s printf( ).
  • format( ) and printf( ) are equivalent.

The Formatter class

  • You can think of Formatter as a translator that converts your format string and data into the desired result.
  • The most useful are PrintStreams, OutputStreams, and Files.

Format specifiers

  • Specifying a width to control the minimum size of a field.
  • precision is used to specify a maximum.

Formatter conversions

  • The ‘b’ conversion works for each variable above. Although it’s valid for any argument type, it might not behave as you’d expect.
  • For any other argument, as long as the argument type is not null the result is always true.

String.format()

  • String.format( ) is a static method which takes all the same arguments as Formatter’s format( ) but returns a String.

Regular expressions

  • They allow you to specify, programmatically, complex patterns of text that can be discovered in an input string.
  • Although the syntax of regular expressions can be intimidating at first, they provide a compact and dynamic language that can be employed to solve all sorts of string processing, matching and selection, editing, and verification problems in a completely general way.

Basics

  • In Java, '\' means "I’m inserting a regular expression backslash, so that the following character has special meaning."
  • The simplest way to use regular expressions is to use the functionality built into the String class.
  • A regular expression doesn’t have to contain special characters.
  • The non-String regular expressions have more powerful replacement tools.
  • Non-String regular expressions are also significantly more efficient if you need to use the regular expression more than once.

Creating regular expressions

  • Once you start writing regular expressions, you’ll often use your code as a reference when writing new regular expressions.

Quantifiers

  • Greedy: A greedy expression finds as many possible matches for the pattern as possible.
  • Reluctant: this quantifier matches the minimum number of characters necessary to satisfy the pattern.
  • Possessive: it generates many states so that it can backtrack if the match fails.

Pattern and Matcher

  • In general, you’ll compile regular expression objects rather than using the fairly limited String utilities.
  • A Pattern object represents the compiled version of a regular expression.
  • You can use the matcher( ) method and the input string to produce a Matcher object from the compiled Pattern object.
  • The Matcher object is then used to access the results, using methods to evaluate the success or failure of different types of matches.

find()

  • Matcher.find( ) can be used to discover multiple pattern matches in the CharSequence to which it is applied.
  • find( ) can be given an integer argument that tells it the character position for the beginning of the search.

Groups

  • Groups are regular expressions set off by parentheses that can be called up later with their group number.

start() and end()

  • Following a successful matching operation, start( ) returns the start index of the previous match, and end( ) returns the index of the last character matched, plus one.
  • While matches( ) only succeeds if the entire input matches the regular expression, lookingAt( ) succeeds if only the first part of the input matches.

Scanning input

  • The usual solution is to read in a line of text, tokenize it, and then use the various parse methods of Integer, Double, etc., to parse the data.
  • With Scanner, the input, tokenizing, and parsing are all ensconced in various different kinds of "next" methods.

Scanner delimiters

  • You can also specify your own delimiter pattern in the form of a regular expression.

Scanning with regular expressions

  • You can also scan for your own user- defined patterns, which is helpful when scanning more complex data.
  • The pattern is matched against the next input token only, so if your pattern contains a delimiter it will never be matched.

Thinking in Java——笔记(13)的更多相关文章

  1. Java笔记13:统计文件中每个字符出现的次数

    一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...

  2. java笔记13之成员变量与局部变量

    成员变量和局部变量的区别 1在类中的位置不同 局部变量:类的方法体内 成员变量:类的方法之外 2内存的不同位置 局部变量:在栈内存中 成员位置:在堆内存 3生命周期不同 局部变量:随着方法的调用而存在 ...

  3. JAVA自学笔记13

    JAVA自学笔记13 1.StringBuffer类 1)线程安全的可变字符序列 线程安全(即同步) 2)StringBuffer与String的区别:一个可变一个不可变 3)构造方法: ①publi ...

  4. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  5. 并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理

    · 在并发编程中,我们有时候会需要使用到线程安全的队列,而在Java中如果我们需要实现队列可以有两种方式,一种是阻塞式队列.另一种是非阻塞式的队列,阻塞式队列采用锁来实现,而非阻塞式队列则是采用cas ...

  6. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  7. 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...

  8. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  9. Java笔记(二十六)……IO流上 字节流与字符流

    概述 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作的数据分为:字节流和字符流 流按流向不同分为:输入流和输出流 IO流常用基类 ...

随机推荐

  1. Android 手机摇一摇功能的实现

    package myapplication.com.myapp.activity; public class Home_Activity extends AppCompatActivity{ //传感 ...

  2. c# 正则表达式用法

    C#正则表达式Regex类的用法 更多2014/2/18 来源:C#学习浏览量:41529 学习标签: 正则表达式 Regex 本文导读:正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串, ...

  3. October 16th Week 43rd Sunday 2016

    Life is not a problem to be solved, but a reality to be experienced. 人生不是待解决的难题,而是等着我们去体验的现实. Life i ...

  4. listView当中有嵌套了有onClickListener的控件时ListView自身的onItemClick无响应的解决方案

    Ref:http://www.cnblogs.com/bluestorm/archive/2013/03/24/2979557.html android:descendantFocusability ...

  5. oracle数据库安装完要做的事情。

    安装数据库客户端.这个在oracle官网可以下载安装(下载链接) 安装PL/SQL PL在下载 配置环境变量(这个比较重要,不配置PLSQL链接不到数据库) 配置的相关环境变量有: 变量名:oracl ...

  6. PCM数据格式,多少字节算一帧

    转自:http://blog.chinaunix.net/uid-9185047-id-3327302.html Somehow i remember that normally 2048 sampl ...

  7. 基于requests实现极客学院课程爬虫

    背景 本文主要是为了完成极客学院课程<Python 单线程爬虫>中讲师布置的实战作业. 开发环境 操作系统:windows 10 Python :Python 2.7 IDE:PyChar ...

  8. uboot的编译

    在我拿到开发板以后,uboot都是编译好的,但是我不知道它是如何生成uboot.bin文件的.经过一番摸索.我也会编译uboot了. #cd /home #mkdir study //创建工作目录 * ...

  9. 【MyEcplise】build workspace卡死

    1.window-Perferences-MyEclipse-Validation 将Manual全部勾掉,Build只留Classpath DependencyValidator,其它全部勾掉. 2 ...

  10. STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱?

    STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱? STM32F系列单片机芯片解密型号: STM32F100  |  STM32F101  |  STM32F102  | ...