Java:字符串拆分数组后按长度存储至多字段(字段限26字符)问题
Java String Splitting Solution for Name Fields (Max 26 Characters Each)
Got it, let's fix this string splitting issue for you! The problem with your current output is that it's not properly splitting the input at word boundaries while respecting the 26-character limit per name field. We need a solution that builds each name field by adding whole words until adding another would exceed the length limit, then moves to the next field.
Step-by-Step Approach
- Split the input string into individual words (handling multiple spaces if needed).
- Iterate through the words, building each name field incrementally.
- Before adding a word to the current field, check if the combined length (including a space separator) stays under 26 characters.
- If adding the word would exceed the limit, finalize the current field and start a new one with the word.
- After processing all words, fill any remaining name fields with empty strings to match your requirement (like
name5="").
Java Code Implementation
public class NameFieldSplitter { public static void main(String[] args) { String fullName = "Bommiraj Sitaramanjaneyulu Rajasekhara Srinivasulu Laxminarayana Siva Venkata Sai"; String[] words = fullName.split("\\s+"); // Split on one or more spaces String[] nameFields = new String[5]; // Holds name1 to name5 int currentFieldIndex = 0; StringBuilder currentField = new StringBuilder(); for (String word : words) { // Calculate length if we add this word to the current field int potentialLength = currentField.length() + (currentField.length() > 0 ? 1 : 0) + // Add space if field isn't empty word.length(); if (potentialLength <= 26) { // Add the word to current field (with space if needed) if (currentField.length() > 0) { currentField.append(" "); } currentField.append(word); } else { // Save current field and start a new one nameFields[currentFieldIndex] = currentField.toString().trim(); currentFieldIndex++; // Stop if we've filled all 5 fields if (currentFieldIndex >= nameFields.length) { break; } // Initialize new field with the current word currentField = new StringBuilder(word); } } // Save the last unfinished field if (currentField.length() > 0 && currentFieldIndex < nameFields.length) { nameFields[currentFieldIndex] = currentField.toString().trim(); } // Fill empty fields with "" for (int i = 0; i < nameFields.length; i++) { if (nameFields[i] == null) { nameFields[i] = ""; } } // Print the result System.out.println("name1=" + nameFields[0]); System.out.println("name2=" + nameFields[1]); System.out.println("name3=" + nameFields[2]); System.out.println("name4=" + nameFields[3]); System.out.println("name5=" + nameFields[4]); } }
Expected Output
When you run this code, you'll get exactly the result you're looking for:
name1=Bommiraj Sitaramanjaneyulu
name2=Rajasekhara Srinivasulu
name3=Laxminarayana Siva Venkata
name4=Sai
name5=
Key Notes
- Uses
\\s+to split the input, which handles cases where there are multiple spaces between words. - Never splits a word across fields—always keeps words intact.
- Explicitly fills unused name fields with empty strings to match your requirement.
- Includes a check to stop processing if we've filled all 5 fields (you can adjust this logic if you need to handle more fields or overflow differently).
内容的提问来源于stack exchange,提问作者Harish Karthick
相关产品推荐
相关产品推荐

