String.split()用法以及特殊分隔符注意,ps:|

本文详细介绍了Java中String.split()方法的使用技巧,包括如何正确处理特殊字符作为分隔符,例如点号、竖线及星号等,并提供了具体实例。

在java.lang包中有String.split()方法,返回是一个数组

我在应用中用到一些,给大家总结一下,仅供大家参考:

1、如果用“.”作为分隔的话,必须是如下写法,String.split("\\."),这样才能正确的分隔开,不能用String.split(".");

2、如果用“|”作为分隔的话,必须是如下写法,String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");

“.”和“|”都是转义字符,必须得加"\\";

3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如,“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");

使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。 

我们看jdk doc中说明  

public String[] split(String regex)

 Splits this string around matches of the given regular expression. 

参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码用竖线 | 分隔字符串,你将得不到预期的结果

 

复制代码
复制代码
   String[] aa = "aaa|bbb|ccc".split("|");

    //String[] aa = "aaa|bbb|ccc".split("\\|"); 这样才能得到正确的结果

    for (int i = 0 ; i <aa.length ; i++ ) {

      System.out.println("--"+aa[i]); 

    } 
复制代码
复制代码

 

用竖 * 分隔字符串运行将抛出java.util.regex.PatternSyntaxException异常,用加号 + 也是如此。

复制代码
复制代码
    String[] aa = "aaa*bbb*ccc".split("*");

    //String[] aa = "aaa|bbb|ccc".split("\\*"); 这样才能得到正确的结果    

    for (int i = 0 ; i <aa.length ; i++ ) {

      System.out.println("--"+aa[i]); 

    }  
复制代码
复制代码

 

显然, + * 不是有效的模式匹配规则表达式,用"\\*" "\\+"转义后即可得到正确的结果。

"|" 分隔串时虽然能够执行,但是却不是预期的目的,"\\|"转义后即可得到正确的结果。

还有如果想在串中使用"\"字符,则也需要转义.首先要表达"aaaa\bbbb"这个串就应该用"aaaa\\bbbb",如果要分隔就应该这样才能得到正确结果,

String[] aa = "aaa\\bbb\\bccc".split("\\\\");

 本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/5162559.html,如需转载请自行联系原作者

error[E0425]: cannot find value `WINDOWS_INITIALIZED` in this scope --> src\main.rs:23:31 | 23 | let mut initialized = WINDOWS_INITIALIZED | ^^^^^^^^^^^^^^^^^^^ not found in this scope warning: unused import: `windows::Devices::Bluetooth::BluetoothAdapter` --> src\main.rs:2:5 | 2 | use windows::Devices::Bluetooth::BluetoothAdapter; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `windows::Devices::Enumeration::DeviceInformation` --> src\main.rs:4:5 | 4 | use windows::Devices::Enumeration::DeviceInformation; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: unused imports: `RadioState` and `Radio` --> src\main.rs:16:31 | 16 | Devices::Radios::{Radio, RadioState}, // 提前导入,避免嵌套块重复导入 | ^^^^^ ^^^^^^^^^^ error[E0601]: `main` function not found in crate `printer_tauri_app` --> src\main.rs:151:2 | 151 | } | ^ consider adding a `main` function to `src\main.rs` error[E0624]: associated function `IBluetoothAdapterStatics` is private --> src\main.rs:42:47 | 42 | let adapter_async = BluetoothAdapter::IBluetoothAdapterStatics(|statics| { | ^^^^^^^^^^^^^^^^^^^^^^^^ private associated function | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Bluetooth\mod.rs:117:5 | 117 | fn IBluetoothAdapterStatics<R, F: FnOnce(&IBluetoothAdapterStatics) -> windows_core::Result<R>>(callback: F) -> windows_core::Result<R> { | --------------------------------------------------------------------------------------------------------------------------------------- private associated function defined here error[E0599]: no method named `GetDefaultAsync` found for reference `&IBluetoothAdapterStatics` in the current scope --> src\main.rs:43:21 | 43 | ... statics.GetDefaultAsync() // 正确调用 GetDefaultAsync(无修改,重点在后续处理) | ^^^^^^^^^^^^^^^ method not found in `&IBluetoothAdapterStatics` error[E0624]: associated function `IDeviceInformationStatics` is private --> src\main.rs:93:32 | 93 | DeviceInformation::IDeviceInformationStatics(|statics: &IDeviceInformationStatics| { | ^^^^^^^^^^^^^^^^^^^^^^^^^ private associated function | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Enumeration\mod.rs:409:5 | 409 | fn IDeviceInformationStatics<R, F: FnOnce(&IDeviceInformationStatics) -> windows_core::Result<R>>(callback: F) -> windows_core::Result<R> { | ----------------------------------------------------------------------------------------------------------------------------------------- private associated function defined here error[E0599]: no method named `FindAllAsyncAqsFilter` found for reference `&IDeviceInformationStatics` in the current scope --> src\main.rs:94:25 | 94 | statics.FindAllAsyncAqsFilter(&selector) // 正确调用,需指定 statics 类型 | ^^^^^^^^^^^^^^^^^^^^^ method not found in `&IDeviceInformationStatics` error[E0425]: cannot find function `extract_bluetooth_address` in this scope --> src\main.rs:134:34 | 134 | let bluetooth_addr = extract_bluetooth_address(&device_id, i); | ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope error[E0560]: struct `BluetoothDevice` has no field named `name` --> src\main.rs:137:17 | 137 | name: device_name, | ^^^^ field does not exist | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Bluetooth\mod.rs:238:12 | 238 | pub struct BluetoothDevice(windows_core::IUnknown); | --------------- `BluetoothDevice` defined here | help: `BluetoothDevice` is a tuple struct, use the appropriate syntax | 136 - inner_devices.push(BluetoothDevice { 137 - name: device_name, 138 - address: bluetooth_addr, 139 - is_paired: true, 140 - rssi: None, 141 - }); 136 + inner_devices.push(BluetoothDevice(/* IUnknown */)); | error[E0560]: struct `BluetoothDevice` has no field named `address` --> src\main.rs:138:17 | 138 | address: bluetooth_addr, | ^^^^^^^ field does not exist | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Bluetooth\mod.rs:238:12 | 238 | pub struct BluetoothDevice(windows_core::IUnknown); | --------------- `BluetoothDevice` defined here | help: `BluetoothDevice` is a tuple struct, use the appropriate syntax | 136 - inner_devices.push(BluetoothDevice { 137 - name: device_name, 138 - address: bluetooth_addr, 139 - is_paired: true, 140 - rssi: None, 141 - }); 136 + inner_devices.push(BluetoothDevice(/* IUnknown */)); | error[E0560]: struct `BluetoothDevice` has no field named `is_paired` --> src\main.rs:139:17 | 139 | is_paired: true, | ^^^^^^^^^ field does not exist | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Bluetooth\mod.rs:238:12 | 238 | pub struct BluetoothDevice(windows_core::IUnknown); | --------------- `BluetoothDevice` defined here | help: `BluetoothDevice` is a tuple struct, use the appropriate syntax | 136 - inner_devices.push(BluetoothDevice { 137 - name: device_name, 138 - address: bluetooth_addr, 139 - is_paired: true, 140 - rssi: None, 141 - }); 136 + inner_devices.push(BluetoothDevice(/* IUnknown */)); | error[E0560]: struct `BluetoothDevice` has no field named `rssi` --> src\main.rs:140:17 | 140 | rssi: None, | ^^^^ field does not exist | ::: C:\Users\86135\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\windows-0.60.0\src\Windows\Devices\Bluetooth\mod.rs:238:12 | 238 | pub struct BluetoothDevice(windows_core::IUnknown); | --------------- `BluetoothDevice` defined here | help: `BluetoothDevice` is a tuple struct, use the appropriate syntax | 136 - inner_devices.push(BluetoothDevice { 137 - name: device_name, 138 - address: bluetooth_addr, 139 - is_paired: true, 140 - rssi: None, 141 - }); 136 + inner_devices.push(BluetoothDevice(/* IUnknown */)); | Some errors have detailed explanations: E0425, E0560, E0599, E0601, E0624. For more information about an error, try `rustc --explain E0425`. warning: `printer-tauri-app` (bin "printer-tauri-app") generated 3 warnings error: could not compile `printer-tauri-app` (bin "printer-tauri-app") due to 11 previous errors; 3 warnings emitted PS D:\Users\2\printer-tauri-app\src-tauri> cargo build warning: unused manifest key: package.check-cfg warning: unused manifest key: source Compiling printer-tauri-app v0.1.0 (D:\Users\2\printer-tauri-app\src-tauri) error: expected one of `:`, `@`, or `|`, found `,` --> src\main.rs:16:49 | 16 | fn extract_bluetooth_address(device_id: &str, index: u32) -> String { | ^ expected one of `:`, `@`, or `|` | = note: anonymous parameters are removed in the 2018 edition (see RFC 1685) help: if this is a parameter name, give it a type | 16 | fn extract_bluetooth_address(device_id: &str: TypeName, index: u32) -> String { | ++++++++++ help: if this is a type, explicitly ignore the parameter name | 16 | fn extract_bluetooth_address(device_id: &_: str, index: u32) -> String { | ++ error: expected one of `!`, `(`, `)`, `,`, `::`, or `<`, found `;` --> src\main.rs:16:45 | 16 | fn extract_bluetooth_address(device_id: &str, index: u32) -> String { | ^ | | | expected one of `!`, `(`, `)`, `,`, `::`, or `<` | help: missing `,` error: expected one of `->`, `where`, or `{`, found `-` --> src\main.rs:16:63 | 16 | fn extract_bluetooth_address(device_id: &str, index: u32) -> String { | ^ expected one of `->`, `where`, or `{` error: could not compile `printer-tauri-app` (bin "printer-tauri-app") due to 3 previous errors 这个报错
最新发布
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值