Home > Enterprise >  Not trimming the un finished word from a string in java
Not trimming the un finished word from a string in java

Time:10-10

In my program I have to trim the phrase in a limit of characters. The code works fine until the phrase is longer then the limit of characters because is not triming the unfinished word.

Example: "The code is not trimming entire character" k=18.

I will return "The code is not tr".

But I need to trim of the last word entirely, to be like "The code is not".

Here is my code:

import java.lang.*;
class Trim{
   public String trim(String message, int K)
   {
       //Checking message non-empty condition, K value and
       //message length
       if(message==""|| message.length()>500 || K>500|| message.substring(0)==" " || message.charAt(message.length()-1) == ' ')
       {
           System.out.println("Message is non-empty and K should have value between 1-500");
           System.exit(0);
       }
       String crop;
 
       //Length of message
       int len=message.length();
 
       //if K value greater than length return message
       if(K>=len)
           return message;
 
       //Crop message until given value
           crop=message.substring(0,K);
 
       //Checking middle value or not
       if((message.substring(K 1)==" ")&&(message.substring(K-1)!=" "))
           return crop;
 
       //Checking crop message with actual length
       if(crop.length()==K)
           return crop;
 
       //Remove last character
       while(crop.length() >0 && crop.charAt(crop.length()-1) != ' '){
           crop=crop.substring(0,crop.length()- 1);
       }
 
       //Remove space
       while(crop.length() >0 && crop.charAt(crop.length()-1) == ' '){
           crop=crop.substring(0, crop.length() - 1);
       }
       return crop;
   }
}

class Main{
 
   public static void main(String[] args) {
 
       Solution s=new Solution();
 
       String message="The code is not trimming entire character";
       System.out.println(s.solution(message,18));
       String message1="no problem";
       System.out.println(s.solution(message1,100));
       String message2="The quick brown fox jumps over the lazy dog";
       System.out.println(s.solution(message2,39));
   }
}

CodePudding user response:

I would simply use substring and lastIndexOf in a while loop to get the string with complete words that is shorter than the limit.

while (input.length() > k) {
    input = input.substring(0, input.lastIndexOf(' ', k));
}

Or as a function

static String trimWords(String sentence, int length) {
    String result = sentence;
    while (result.length() > length) {
        result = result.substring(0, result.lastIndexOf(' ', length));
    }
    return result;
}

CodePudding user response:

Try this.

static String trim(String message, int k) {
    return message.replaceFirst("^(.{0,"   k   "})\\b.*",  "$1");
}

public static void main(String[] args) {
    String s = "The code is not trimming entire character";
    for (int k = 12; k <= 25;   k)
        System.out.println("k="   k   " : "   trim(s, k));
}

output:

k=12 : The code is 
k=13 : The code is 
k=14 : The code is 
k=15 : The code is not
k=16 : The code is not 
k=17 : The code is not 
k=18 : The code is not 
k=19 : The code is not 
k=20 : The code is not 
k=21 : The code is not 
k=22 : The code is not 
k=23 : The code is not 
k=24 : The code is not trimming
k=25 : The code is not trimming 
  •  Tags:  
  • java
  • Related