This article mainly introduces the relevant information about the implementation examples of java string splicing. I hope that through this article, everyone can master the implementation of character splicing. Friends in need can refer to
java string Implementation example of splicing
In actual development work, string processing is the most common programming task. This question requires the program to process the string input by the user. The specific rules are as follows:
1. Change the first letter of each word to uppercase.
2. Separate numbers and letters with underline characters (_) to make them clearer
3. Adjust multiple spaces in the middle of words to one space.
For example:
User input:
you and me what cpp2005program
Then the program output:
You And Me What Cpp_2005_program
User input:
this is a 99cat
Then the program output:
This Is A 99_cat
We assume that the string entered by the user only contains lowercase letters, spaces and numbers, and does not contain others. letters or symbols. Each word is separated by 1 or more spaces.
Assume that the length of the string entered by the user does not exceed 200 characters.
Require candidates to write all classes in one file. After debugging, just save it in the "Answer.txt" corresponding to the question number in the candidate's folder. Do not copy in relevant project files. Please do not use package statements.
In addition, only the syntax or calls allowed in JDK1.5 can appear in the source program. Cannot use version 1.6 or higher.
Implementation example:
import java.util.ArrayList; import java.util.Scanner; //you and me what cpp2005program //则程序输出: //You And Me What Cpp_2005_program public class Main { public static void main(String[] args){ String x = new Scanner(System.in).nextLine(); combineStr(x); } public static void combineStr(String x){ //获取处理后的数据集合 ArrayList<String> list = repair(x); //用于判断数字的字符串 String intStr = "0123456789"; //获取每个字符串进行字符的拼接转换 String result = ""; for(int i=0;i<list.size();i++){ //取出一个字符 String temp = list.get(i); //初始化结果值 result = temp; //进行数字与字母的判断 for(int k=0;k<temp.length()-1;k++){ if(intStr.indexOf(temp.charAt(k))!=-1 && intStr.indexOf(temp.charAt(k+1))==-1){ // 此时判断条件为数字 // 8a 返回替换后的字符,原字符不变 result = result.replace(temp.substring(k, k+2), (temp.charAt(k)+"_"+temp.charAt(k+1))); }else if(intStr.indexOf(temp.charAt(k))==-1 && intStr.indexOf(temp.charAt(k+1))!=-1){ //字母数字 result = result.replace(temp.substring(k, k+2), (temp.charAt(k)+"_"+temp.charAt(k+1))); } } System.out.print(result+" "); } } //获取用户输入的,去掉重复的空白符 public static ArrayList<String> repair(String x){ //保存首字符转为大写后的单词 ArrayList<String> list = new ArrayList<String>(); //用于判断数字的字符串 String intStr = "0123456789"; String[] arr = x.split(" "); for(int i=0;i<arr.length;i++){ if(!arr[i].equals("")){ //对每一个字符进行判断 if( intStr.indexOf(arr[i].charAt(0))==-1){ // 对应的字符为字母而不是数字,==-1表示没有找到数字,则为字母 String newString = (arr[i].charAt(0)+"").toUpperCase()+arr[i].substring(1); list.add(newString); }else{ list.add(arr[i]); } } } return list; } }
The above is the detailed content of Introduction to the method of splicing strings in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
