About string building, connecting, finding

Source: Internet
Author: User

Building, connecting, and finding questions about strings in Java

1. What are the methods for building a string, here are some ways to build a string: 1, String str= "abc" 2, String Str=new string ("abc");

3, StringBuffer bf=new stringbuffer ("abc");

buf=bf.tostring;

4, StringBuilder bu=new StringBuilder ("abc");

bu=bu.tostring;

2. What are the connection methods for strings? What methods are more efficient?

First analyze our most commonly used string connection method:

1, String str= "abc"; str+= "Def";

Using the "+" symbol to connect, the symbol is already overloaded in Java for us to use, so we usually use this method to concatenate strings. This method is very convenient, but what about the efficiency of this approach? Here is a way to provide a comparison. Concatenate strings using the Concat () method in string

2, Str1.concat ("abc");

Both of these methods can connect strings, which is more efficient? Let's take a look.

In 100 connections, the time difference is small.

When the number of connections reached 100,000 times, the efficiency difference is very obvious.

Now to explore the implementation of the two methods of connection, first of all, the use of symbolic "+" connection. When we use this method, we write the code,

String a= "abc"; string b= "def"; a+=b;

How many objects have been opened here? Do not think that there are only two objects, in fact, the opening of three objects, also opened up three memory areas.

A memory area B memory Area C memory Area

The way to connect is to copy the data of A and B memory area to the C memory area. So this approach is to constantly open up memory to store the new concatenated strings, the time complexity is: Nx

The second method, how it is, the use of the Concat method of the string class usually requires this:

String a= "abc"; string b= "Def"; A=a.concat (b);

There are only two objects opened, that is, the opening up of two of memory,

A memory area B memory Area

It is only necessary to point the end of the a memory area to the head of the B memory area to realize the connection of two strings, so the time complexity is: N is a constant degree of complexity. So the efficiency of these two methods is higher, the must not is simple and clear.

The following is a description of the two uncommon string connection methods:

1. Using StringBuffer class

2. Using StringBuilder class

String connections using these two classes are also very efficient,

This can be written when using StringBuffer,

StringBuffer bf=new StringBuffer ("abc");

Bf.append ("abc");

String stringbf=bf.tostring ();

A similar notation when using StringBuilder:

StringBuilder bu=new StringBuilder ("abc");

Bu.append ("abc");

String stringbf=bu.tostring ();

The functions of StringBuffer and StringBuilder are basically the same, except that StringBuffer is thread-safe and StringBuilder is not thread-safe. As a result, StringBuilder will be more efficient. About the principle here is not too much to introduce.

In terms of string lookup problems, it is a question of whether a string of B is to be judged in a. Encapsulation in Java has several methods for users to use:
1, int indexOf (String str): Returns the first occurrence of the specified substring in this string, the position of the integer (the first position is 0).
2, int indexOf (String str, int startIndex): Returns the position of the first occurrence of the specified substring in this string, starting at the specified index (the first position is 0).
3, int lastIndexOf (string str): Returns the positional integer of the rightmost occurrence of the specified substring in this string (the first position is 0).
4, int lastIndexOf (String str, int startIndex): Searches backward at the specified index, returning the integer of the position of the specified substring that last occurred in this string (the first position is 0).

For more efficient string lookup algorithms you can refer to the blog: http://www.nowamagic.net/algorithm/algorithm_KmpFastStringSearch.php

About string building, connecting, finding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.