Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the considerations of common strings in Java language

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java language common string points for attention, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Take out the spaces before and after a string

For example, there is a product management window in an inventory management system. In this window, there is product number, serial number and other information. For the accuracy of this information, users require that there can be no spaces before and after the product number, serial number and other fields when the data is saved. Because the following product picture address and other information are automatically compiled according to the product number. If there is a space after the product number, there will be a problem with the URL address of the product picture (the product picture is not placed in the database, but on a file server on the network, and the corresponding picture on the file server is opened by the URL address). If there is a problem with this URL address, it is obvious that the picture will not open properly.

But in practice, many users inadvertently add a few spaces before or after the product number, resulting in a difference between the URL address and the actual picture address. There are also users who may copy numbers directly from tables such as Excle, and these tools have something to do with format control or other reasons, often containing hidden spaces. If you copy directly from these tools to the application, spaces will be automatically inserted later. For the sake of coding this kind of error, the program developer may want to filter the contents of common strings in the Java language when saving them to the database. If you find a space before and after a common string in the Java language, remove the space before saving it to the database. In this way, you can ensure that there are no space characters before and after the product number. Although this can also be done in the database, it is troublesome to deal with it together, and the author suggests that the problem be solved at the application level.

In the Java language platform, there happens to be a ready-made way to deal with it. The Java language contains many string handling functions or methods that are common in the Java language, and the trim () method is one of them. This method ignores the leading and trailing spaces of the input string and finally returns a copy of the string with the leading and trailing spaces removed. Simply put, this method is to determine whether a string has leading and trailing spaces. If so, remove them and return string data that does not contain trailing spaces and header spaces. There are three issues to pay attention to when using this method to filter space characters. One is that this method only takes out leading spaces and trailing spaces, not the middle spaces. Second, normally, the tab key also forms a series of space characters, which are also filtered out by the trim () method. Third, when using this method, there must be explicit instructions from the user. That is, in some cases, users may need to have these spaces. In order to improve the flexibility of the application, the author's opinion is to set a dynamic parameter in the design of the window or document. It is up to the user to set whether spaces need to be removed when saving. In this way, the flexibility of the application can be improved. When users need different requirements, it does not need to be done through secondary development.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report