An implementation instance of java string concatenation
- 2020-09-28 08:53:04
- OfStack
An implementation instance of java string concatenation
In real development, dealing with strings is the most common programming task. This topic is to require the program to user input string processing. Specific rules are as follows:
Capitalize the first letter of each word.
2. Separate the Numbers from the letters with an underscore character (_) for clarity
3. Change the number of Spaces in the middle of a word to one.
Such as:
User input:
you and me what cpp2005program
Program output:
You And Me What Cpp_2005_program
User input:
this is a 99cat
Program output:
This Is A 99_cat
We assume that the string entered by the user contains only lowercase letters, Spaces, and Numbers, and no other letters or symbols. Each word is separated by one or more Spaces.
Assume that the user enters a string of no more than 200 characters in length.
The examinee is required to write all the classes in one document. After debugging, it can be stored in the "Answer. txt" corresponding to the question number under the examinee folder. Do not copy the relevant project documents. Do not use the package statement.
In addition, only the syntax or calls allowed in JDK1.5 can appear in the source program. Version 1.6 or later is not available.
Implementation examples:
import java.util.ArrayList;
import java.util.Scanner;
//you and me what cpp2005program
// Program output:
//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){
// Gets the processed data set
ArrayList<String> list = repair(x);
// A string used to determine a number
String intStr = "0123456789";
// Gets each string for a character concatenation conversion
String result = "";
for(int i=0;i<list.size();i++){
// Take out the 1 A character
String temp = list.get(i);
// Initialize the resulting value
result = temp;
// Make numerical and alphabetic judgments
for(int k=0;k<temp.length()-1;k++){
if(intStr.indexOf(temp.charAt(k))!=-1 && intStr.indexOf(temp.charAt(k+1))==-1){ // The judgment condition is a number
// 8a Returns the replaced character, unchanged
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){ // An alphanumeric
result = result.replace(temp.substring(k, k+2), (temp.charAt(k)+"_"+temp.charAt(k+1)));
}
}
System.out.print(result+" ");
}
}
// Gets user input and removes duplicate white space characters
public static ArrayList<String> repair(String x){
// Save the word after the first character is capitalized
ArrayList<String> list = new ArrayList<String>();
// A string used to determine a number
String intStr = "0123456789";
String[] arr = x.split(" ");
for(int i=0;i<arr.length;i++){
if(!arr[i].equals("")){
// For each 1 Three characters to judge
if( intStr.indexOf(arr[i].charAt(0))==-1){ // The corresponding characters are letters rather than Numbers ,==-1 If no number is found, it is a letter
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 example of java string concatenation, if you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!