Cut string, js split string

Source: Internet
Author: User

Cut string, js split string

First, blow the intention: cut the string because when the image address is saved in the database, a commodity may have multiple images. We can create a commodity table (goods) and an image table (goods_image), and then use the id of the image table as the foreign key of the commodity table to implement one-to-many Association.

However, if you want to create a table with a path and a path, there will be a lot of data. Since you can add a field (goods_image_url) to the product table, why not?

 

Next, let's talk about the idea: The image address is spliced together and then divided into a separate image address. In this case, a separator is used to separate each address before splicing. The split address can be received by using the List <String> set or the Arrays array.

 

So I wrote a tool class: StringToArrays

1 public class StringToArrays {2 3 String [] arrayStr; 4 6 public String [] staMethod (String str, String separator) {7 8 arrayStr = str. split (separator); // The split character is separator, and then the result is sent to array 9 return arrayStr; 10} 11}

As you can see, the code is actually very simple, that is, defining an array to receive the address value after being split. Then define a method. Since it is intended to be generic, two parameters must be input in the method. The first parameter is string data, and the second parameter is a separator. If someone else calls the parameter, I have to return a parsed string array.

The tool class has been written. When the Android end obtains the address value from the database, it calls the method of the tool class above to obtain the address array, then, you can know the length of the array through the length method of the array and other operations. Finally, you can display network images by loading some plug-ins or third-party tools.

Related Article

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.