添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
深沉的书包  ·  hjapp1 — 目录·  2 月前    · 
失眠的围巾  ·  Singapore Standards·  3 月前    · 
英俊的紫菜  ·  Spring ...·  4 月前    · 
Partner – Microsoft – NPI EA (tag= Azure)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, you can get started over on the documentation page .

And, you can also ask questions and leave feedback on the Azure Container Apps GitHub page .

Partner – Codium – NPI EA (site = Main Site)
announcement - icon

Get non-trivial analysis (and trivial, too!) suggested right inside your IDE or Git platform so you can code smart, create more value, and stay confident when you push.

Get CodiumAI for free and become part of a community of over 280,000 developers who are already experiencing improved and quicker coding.

Write code that works the way you meant it to:

CodiumAI. Meaningful Code Tests for Busy Devs

Partner – DBSchema – NPI EA (tag = Spring Data JPA)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema .

The way it does all of that is by using a design model , a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Take a look at DBSchema

Partner – Aegik AB – NPI EA (cat=JPA)
announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only , so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server , hit the record button, and you'll have results within minutes:

out the Profiler

eBook – Guide Junit – NPI EA (cat=Java)
announcement - icon

A quick guide to materially improve your tests with Junit 5:

>> The Junit 5 handbook
eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – RwS – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

REST With Spring (new)

Course – LS – NPI EA (cat=Spring)
announcement - icon

Get started with Spring and Spring Boot, through the reference Learn Spring course:

>> LEARN SPRING

Partner – Codium – NPI EA (cat = Testing)
announcement - icon

Get non-trivial analysis (and trivial, too!) suggested right inside your IDE or Git platform so you can code smart, create more value, and stay confident when you push.

Get CodiumAI for free and become part of a community of over 280,000 developers who are already experiencing improved and quicker coding.

Write code that works the way you meant it to:

CodiumAI. Meaningful Code Tests for Busy Devs

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Looking for the ideal Linux distro for running modern Spring apps in the cloud?

Meet Alpaquita Linux : lightweight, secure, and powerful enough to handle heavy workloads.

This distro is specifically designed for running Java apps . It builds upon Alpine and features significant enhancements to excel in high-density container environments while meeting enterprise-grade security standards.

Specifically, the container image size is ~30% smaller than standard options, and it consumes up to 30% less RAM:

Alpaquita Containers now.

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth , to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project .

You can explore the course here:

>> Learn Spring Security

Partner – DBSchema – NPI EA (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema .

The way it does all of that is by using a design model , a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Take a look at DBSchema

Partner – Aegik AB – NPI EA (tag = SQL)
announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only , so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server , hit the record button, and you'll have results within minutes:

out the Profiler

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot .

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

When processing text containing comma-separated-values, it may be necessary to ignore commas that occur in quoted sub-strings.

In this tutorial, we’ll explore different approaches for ignoring commas inside quotes when splitting a comma-separated String .

2. Problem Statement

Suppose we need to split the following comma-separated input:

String input = "baeldung,tutorial,splitting,text,\"ignoring this comma,\"";

After splitting this input and printing the result, we’d expect the following output:

baeldung
tutorial
splitting
"ignoring this comma,"

In other words, we cannot consider all comma characters as being separators. We must ignore the commas that occur inside quoted sub-strings.

3. Implementing a Simple Parser

Let’s create a simple parsing algorithm:

List<String> tokens = new ArrayList<String>();
int startPosition = 0;
boolean isInQuotes = false;
for (int currentPosition = 0; currentPosition < input.length(); currentPosition++) {
    if (input.charAt(currentPosition) == '\"') {
        isInQuotes = !isInQuotes;
    else if (input.charAt(currentPosition) == ',' && !isInQuotes) {
        tokens.add(input.substring(startPosition, currentPosition));
        startPosition = currentPosition + 1;
String lastToken = input.substring(startPosition);
if (lastToken.equals(",")) {
    tokens.add("");
} else {
    tokens.add(lastToken);

Here, we start by defining a List called tokens, which is responsible for storing all the comma-separated values.

Next, we iterate over the characters in the input String.

In each loop iteration, we need to check if the current character is a double quote. When a double quote is found, we use the isInQuotes flag to indicate that all upcoming commas after the double quotes should be ignored. The isInQuotes flag will be set false when we find enclosing double-quotes.

A new token will be added to the tokens list when isInQuotes is false, and we find a comma character. The new token will contain the characters from startPosition until the last position before the comma character.

Then, the new startPosition will be the position after the comma character.

Finally, after the loop, we’ll still have the last token that goes from startPosition to the last position of the input. Therefore, we use the substring() method to get it. If this last token is just a comma, it means that the last token should be an empty string. Otherwise, we add the last token to the tokens list.

Now, let’s test the parsing code:

String input = "baeldung,tutorial,splitting,text,\"ignoring this comma,\"";
var matcher = contains("baeldung", "tutorial", "splitting", "text", "\"ignoring this comma,\"");
assertThat(splitWithParser(input), matcher);

Here, we’ve implemented our parsing code in a static method called splitWithParser. Then, in our test, we define a simple test input containing a comma enclosed by double quotes. Next, we use the hamcrest testing framework to create a contains matcher for the expected output. Finally, we use the assertThat testing method to check if our parser returns the expected output.

In an actual scenario, we should create more unit tests to verify the behavior of our algorithm with other possible inputs.

4. Applying Regular Expressions

Implementing a parser is an efficient approach. However, the resulting algorithm is relatively large and complex. Thus, as an alternative, we can use regular expressions.

Next, we will discuss two possible implementations that rely on regular expressions. Nevertheless, they should be used with caution as their processing time is high compared to the previous approach. Therefore, using regular expressions for this scenario can be prohibitive when processing large volumes of input data.

4.1. String split() Method

In this first regular expression option, we’ll use the split() method from the String class. This method splits the String around matches of the given regular expression:

String[] tokens = input.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);

At first glance, the regular expression may seem highly complex. However, its functionality is relatively simple.

In short, using positive lookahead, tells to split around a comma only if there are no double quotes or if there is an even number of double quotes ahead of it.

The last parameter of the split() method is the limit. When we provide a negative limit, the pattern is be applied as many times as possible, and the resulting array of tokens can have any length.

4.2. Guava’s Splitter Class

Another alternative based on regular expressions is the use of the Splitter class from the Guava library:

Pattern pattern = Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
Splitter splitter = Splitter.on(pattern);
List<String> tokens = splitter.splitToList(input);

Here, we are creating a splitter object based on the same regular expression pattern as before. After creating the splitter, we use the splitToList() method, which returns a List of tokens after splitting the input String.

5. Using a CSV Library

Although the alternatives presented are interesting, it may be necessary to use a CSV parsing library such as OpenCSV.

Using a CSV library has the advantage of requiring less effort, as we don’t need to write a parser or a complex regular expression. As a result, our code ends up being less error-prone and easier to maintain.

Moreover, a CSV library may be the best approach when we are not sure about the shape of our input. For example, the input may have escaped quotes, which would not be properly handled by previous approaches.

To use OpenCSV, we need to include it as a dependency. In a Maven project, we include the opencsv dependency:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.8</version>
</dependency>

Then, we can use OpenCSV as follows:

CSVParser parser = new CSVParserBuilder()
  .withSeparator(',')
  .build();
CSVReader reader = new CSVReaderBuilder(new StringReader(input))
  .withCSVParser(parser)
  .build();
List<String[]> lines = new ArrayList<>();
lines = reader.readAll();
reader.close();

Using the CSVParserBuilder class, we start by creating a parser with a comma separator. Then, we use the CSVReaderBuilder to create a CSV reader based on our comma-based parser.

In our example, we provide a StringReader as an argument to the CSVReaderBuilder constructor.  However, we can use different readers (e.g., a file reader) if required.

Finally, we call the readAll() method from our reader object to get a List of String arrays. Since OpenCSV is designed to handle multi-line inputs, each position in the lines list corresponds to a line from our input. Thus, for each line, we have a String array with the corresponding comma-separated values.

Unlike previous approaches, with OpenCSV, the double quotes are removed from the generated output.

6. Conclusion

In this article, we explored multiple alternatives for ignoring commas in quotes when splitting a comma-separated String. Besides learning how to implement our own parser, we explored the use of regular expressions and the OpenCSV library.

As always, the code samples used in this tutorial are available over on GitHub.

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Looking for the ideal Linux distro for running modern Spring apps in the cloud?

Meet Alpaquita Linux: lightweight, secure, and powerful enough to handle heavy workloads.

This distro is specifically designed for running Java apps. It builds upon Alpine and features significant enhancements to excel in high-density container environments while meeting enterprise-grade security standards.

Specifically, the container image size is ~30% smaller than standard options, and it consumes up to 30% less RAM:

Alpaquita Containers now.

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Just published a new writeup on how to run a standard Java/Boot application as a Docker container, using the Liberica JDK on top of Alpaquita Linux:

Spring Boot Application on Liberica Runtime Container.

Partner – Aegik AB – NPI EA (tag = SQL)
announcement - icon

Slow MySQL query performance is all too common. Of course it is.

The Jet Profiler was built entirely for MySQL, so it's fine-tuned for it and does advanced everything with relaly minimal impact and no server changes.

out the Profiler

Partner – Codium – NPI EA (cat = Testing)
announcement - icon

Explore the secure, reliable, and high-performance Test Execution Cloud built for scale. Right in your IDE:

CodiumAI. Meaningful Code Tests for Busy Devs

Basically, write code that works the way you meant it to.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Download the Guide
eBook – Video Security – NPI EA (cat=Spring Security)
access to the video
eBook – Persistence – NPI EA (cat=Persistence)