【Java实战经验】根据大写字母分割字符串,并保留大写字母

华为OD机试题,用 Java 解【字符串分割】问题 字符串分割 给定一个非空字符串 S,其被 N 个‘-’分隔成 N+1 的子串,给定正整数 K,要求除第一个子串外,其余的子串每 K 个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。 输入输出描述: 输入描述: 输入为两行,第一行为参数 K,第二行为字符串 S。 输出描述: 输出转换后的字符串。 阅读详情

◉ 需求背景:

在使用反射操作数据库时,实体类属性名和数据库字段名命名风格不一样(属性名为驼峰命名,字段名使用下划线)
这时就需要将两个名称进行互换,其中将属性名转化成字段名就需要用到以大写字母分割字符串这个方法。


◉ 实现效果:

在这里插入图片描述


◉ 实现代码:

String tempName = "MyNameIsIceClean".replaceAll("[A-Z]", "_$0");
String[] partName = tempName.split("_");
for (String name : partName){
	System.out.print(name + " ");
}

◉ 代码解析:

方法 replaceAll 可以使用正则表达式对字符串进行修改,其中:

  • [A-Z] 表示任意一个大写字母
  • $0 表示匹配到的第一个字符(即该大写字母)
  • _$0 表示在匹配到的大写字母前加一个下划线(作为分隔符使用)
  • 最后再使用方法 split 以下划线 ’_‘ 为分隔符分割字符串,得到结果

每天进步一点点(IceClean)

华为OD机试D卷C卷 - 字符串分割转换(C++ Java JavaScript Python C语言) 给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。 阅读详情

相关推荐

【华为OD机考真题】- 字符串分割(2025B卷-100分)(Java版)

🔔本文收录于「最新最全华为OD机试真题(Java版)」专栏,手把手带你零基础教学华为OD机试。本题集提供最优题解思路,解题步骤,代码解析,复杂度分析及最优题解源码等,支持多语言题解,助你轻松拿捏OD机考,一举上岸!安利大家关注&&收藏&&订阅!题库正在疯狂收录中,up!up!up!! 🚫提醒:拒绝一切代考/替考,违法必究!本人所写题库均精心搜集于互联网。    💗订阅福利:一次订阅,可永久免费阅读,提供在线答疑解惑,后续题库更新皆可阅读使用!

**My Coding Family** 1212

java首字符是字母下划线_我们如何在Java字符串中的每个大写字母之前添加下划线?...

使用StringBuffer类使用StringBuffer在字符串的每个大写字母之前添加下划线-创建一个空的StringBuffer对象。Character类的isUpperCase()方法接受一个字符并验证其是否为大写,如果是,则此方法返回true。使用此方法,验证字符串中的每个字符。如果是大写字母,请使用append()方法在其前附加下划线。示例publicclassAdding_Befo...

weixin_35734408的博客 1329

【华为OD机试】真题E卷-字符串分割2(JAVA)

给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。

u014481728的博客 1182

python把一串英文进行字母分割

你可以使用 Python 中的 re 模块来分割字符串。 例如,你可以使用下面的代码来分割一个字符串: import restring = "This is a test string" # 使用正则表达式来分割字符串 # 这里的 `\b` 表示单词边界,也就是每个单词的开头和结尾 split_string = re.split(r'\b', string) print(split_strin...

weixin_42601134的博客 1910

华为OD机试C卷-- 字符串分割(二)(Java & JS & Python)

给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。 对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母; 反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。

wbajsjhhhhh的博客 3383

Java类Rectangle实现打印字符串中的大写字母

本文还有配套的精品资源,点击获取 简介:在Java中,创建一个类来识别并打印出给定字符串中的大写字母,有助于理解和掌握字符串处理以及面向对象编程的基本概念。这个示例中,我们用名为 Rectangle 的类来演示如何处理字符串。尽管类名通常代表某种实体或概念,但在这里它主要用于说明字符串操作。我们遍历字符串中的每个字符,使用 Character.isUp...

weixin_35871529的博客 779

驼峰和下划线互转

public class HumpLineUtil { private static Pattern humpPattern = Pattern.compile("[A-Z]"); private static Pattern linePattern = Pattern.compile("(_)(\\w)"); /** * 驼峰转...

turbo_sky的博客 777

别再搞混了!MATLAB通信仿真中EbN0转SNR的保姆级公式推导与避坑指南

本文深入解析MATLAB通信仿真中EbN0与SNR转换的核心公式,揭示数字仿真与理论系统的关键差异。通过真实案例展示错误参数设置导致的隐蔽问题,并提供完整的参数自查清单和正确实现代码,帮助工程师和学生避免常见错误,确保仿真结果准确可靠。

weixin_28746223的博客 348

java 分割字符串(多种方法)

toc]

Chen_sj的博客 2万+

uniapp map地图实现marker聚合点,并点击marker触发事件

【代码】uniapp map地图实现marker聚合点,并点击marker触发事件。

weixin_44705979的博客 5437

Cesium+Three.js融合实战:从零构建GIS与BIM一体化场景

本文详细介绍了如何将Cesium与Three.js进行技术融合,从零构建GIS与BIM一体化场景。通过环境配置、核心场景搭建、坐标转换、性能优化及交互处理等实战步骤,阐述了Cesium负责宏观地理空间渲染、Three.js处理微观精细BIM模型的协同方案,为智慧城市、数字孪生等应用提供关键技术实现路径。

weixin_29230865的博客 460

java截取字符串,截串,substring和split,分割字母和数字,正则缝隙

需求,把"01:大汽车",分成01和大汽车 有两种做法:一是substring package test; public class substringTest { public static void main(String args[]) { String N = "01:大汽车"; String L=""; String R=""; i...

黑色头发 2645

DAC8568控制器实战:如何用STM32实现多通道同步输出(附完整代码)

本文详细介绍了如何使用STM32微控制器驱动TI的DAC8568多通道DAC控制器,实现高精度的多通道同步模拟信号输出。内容涵盖硬件设计要点、SPI时序精准配置、驱动层命令构建,并重点解析了通过LDAC引脚实现同步更新的核心策略,提供了完整的代码框架和实战案例,帮助工程师快速解决多通道同步输出的工程难题。

weixin_29179311的博客 170

java 按 大写字母_用大写字母拆分字符串

回答(12)2 years ago这是另一种正则表达式解决方案 . 该问题可以被称为“如何在执行拆分之前在每个大写字母之前插入空格”:>>> s = "TheLongAndWindingRoad ABC A123B45">>> re.sub( r"([A-Z])", r" \1", s).split()['The', 'Long', 'And', 'Windin...

weixin_39970823的博客 1694

Trae AI开发工具:大前端环境配置与避坑指南

大前端开发的第一步往往不是选框架,而是搭建一套可靠的工具链。IDE作为编码、调试与AI能力协同的载体,直接决定后续开发的顺畅度。传统编辑器需要手动拼装插件生态,而新一代AI开发工具将对话、代码生成与项目脚手架能力内置到编辑器底层,降低了环境准备的门槛。Trae基于VS Code内核,兼容其插件体系,同时提供Chat、Builder、内联对话三种AI交互模式,适合从VS Code迁移的开发者,也适合大前端新人快速上手。安装时需认准官方渠道,注意Node.js、Git版本联动,配置自动格式化与手动更新策略,并结

weixin_26833139的博客 281

大疆航线文件解析指南:5分钟搞懂KMZ里的template.kml和waylines.wpml区别

本文深入解析大疆无人机航线文件KMZ中template.kml与waylines.wpml的核心区别。template.kml是定义任务框架和全局参数的可编辑模板,而waylines.wpml是包含精确执行指令的最终文件。理解两者差异对于高效规划航线、批量生成任务及进行二次开发至关重要。

sun99的博客 866

python字符串大写字母个数_【python实例】统计字符串里大写字母,小写字母的个数和非字母的个数...

"""给定一个以下字符串:统计大写字母的个数,小写字母的个数,非字母的个数。str1 = "ajdkkKDKEK1343KFKiriromfkfKKRIOWJF0928398jjee11djdDJJRH""""str1 = "ajdkkKDKEK1343KFKiriromfkfKKRIOWJF0928398jjee11djdDJJRH"big_num = 0 # 记录大写字母个数small_nu...

weixin_39995280的博客 2万+

微信小程序:errno: 1500104, errCode: 10008, errMsg: “writeBLECharacteristicValue:fail:system error, status

微信小程序errno: 1500104, errCode: 10008, errMsg: "writeBLECharacteristicValue:fail:system error, status: UNKNOWN (239)"微信小程序调用蓝牙API,wx.writeBLECharacteristicValue()报错,微信小程序蓝牙写入错误,在开发uniapp微信小程序蓝牙通信功能时报错,蓝牙开发设备控制核心api,微信小程序调用蓝牙API “wx.writeBLECharacteristicValu

✅ 精选推荐文章 3930

java传入一个字符串 将它分割成大写字符为首的字符串数组

/*      * 传入一个字符串 将它分割成大写字符为首的字符串数组      */     private ArrayList splitByUpperCase(String str) {         ArrayList rs = new ArrayList();         int index = 0;         int len = str.length();  

tw19811220的专栏 3370

手把手教你学pcie--用Verilog搭建PCIe控制器核心模块

PCIe控制器是硬件设计的核心模块,负责实现设备与主机之间的高速通信。本文将以Verilog语言为例,分步骤讲解如何从零构建一个简化的PCIe控制器核心模块,涵盖事务层(TLP)生成、数据链路层(DLLP)处理和物理层(PHY)接口适配。目标是为嵌入式开发者、FPGA工程师提供一个可扩展的硬件设计框架。​。

MHD0815的博客 340

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

字符串分割

算法题,字符串分割

qq_44813449的博客 364

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

华为机试-0001-字符串分割

华为机试-0001-字符串分割

weixin_43873121的博客 1101

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

JAVA求解【字符串分割】

给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。输入为两行,第一行为参数K,第二行为字符串S。输出转换后的字符串。3 12abc-abCABc-4aB@12abc-abc-ABC-4aB-@子串为12ab

Winyar的博客 708
上一篇: 【Java.bug】(1)Can‘t find bundle for base name xxx, locale zh_CN
下一篇: 【环境配置】为 idea 配置 Tomcat 环境(全操作)
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒冰小澈IceClean

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值