Home >Java >javaTutorial >Introduction to the method of splicing strings in Java

Introduction to the method of splicing strings in Java

黄舟
黄舟Original
2017-09-02 11:09:211847browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn