Java String.split() 关于空值(empty results)

本文详细解析了Java中String类的split方法使用细节,包括默认行为、如何保留空字符串及通过limit参数控制分割结果等。并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现象

String testString1 = "1, 1, 0, 0, 0, 6.17, 3.50,,,,,,,,,,,,,1";
String testString2 = "1, 1, 0, 0, 0, 6.17, 3.50,,,,,,,,,,,,,";

采用默认的split方式

testString.split(",");

这两者的结果是不一致的,前者的长度是20,后者为7. 因为字符串中产生了空值。如果头和尾都有值,中间空,长度不影响。否则split结果会去掉空值。

细看下split的API

public String[] split(String regex,
int limit)
Splits this string around matches of the given regular expression.
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array’s length will be no greater than n, and the array’s last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

从API来看,核心有两点

  1. 第一个参数是正则表达式
  2. 第二个limit参数指定分割结果数目

解决方案

保留所有空值
String[] tpStrings = testString.split(",",-1);

不管空值出现在哪里,都不会丢弃

去掉空值
String[] tpStrings = testString.split(",+");

就是中间出现空值,也会舍弃

### 实现Electron应用窗口最大化的方案 在Electron中,可以通过`BrowserWindow`实例对象来控制窗口的行为,其中包括最大化操作。具体来说,可以使用`maximize()`方法让当前窗口达到屏幕的最大尺寸[^1]。 对于创建一个具备最大化能力的窗口而言,首先应当构建基本的窗口结构: ```javascript const { app, BrowserWindow } = require('electron'); let mainWindow; function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } }); mainWindow.loadURL(`file://${__dirname}/index.html`); } app.on('ready', createWindow); ``` 为了使该窗口能够响应用户的最大化请求,在HTML文件内嵌入按钮或其他触发器,并通过IPC(进程间通信)向主进程中传递消息以执行最大化命令: ```html <button id="maximize">Maximize</button> <script> require('./renderer.js'); </script> ``` 接着是在渲染进程中处理点击事件并向主进程发送信号: ```javascript // renderer.js document.getElementById('maximize').addEventListener('click', () => { const { ipcRenderer } = require('electron'); ipcRenderer.send('window-maximize'); }); ``` 最后一步就是在主进程中监听来自渲染端的消息并作出相应动作: ```javascript // main.js const { ipcMain } = require('electron'); ipcMain.on('window-maximize', (event) => { if (!mainWindow.isMaximized()) { mainWindow.maximize(); } else { mainWindow.unmaximize(); // 如果已经处于最大化状态,则恢复原状 } }); ``` 上述代码展示了如何在一个简单的Electron应用程序里实现窗口最大化的功能[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值