Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
since_1995
Contributor III

Split the string after 40 character

I am extracting the name from Snowflake.

Name has more than 40 characters. To make this easy, I am going to split the name and put the first 40 characters in Col1 and the rest in Col 2. I'm worried about splitting the string in the middle of a word.

For example:

Name is : " Talend Advantage International Consulting Solutions LLC"

here i have 55 characters,

If i split the first 40 characters in col1 it will be "Talend Advantage International Consultin"

Instead, i want to have "Talend Advantage International" in col1 and

"Solutions LLC" in col2

I want to avoid the middle word splitting.

Please help!!

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

A routine like this will work. I've just knocked it up and it will need refining (it doesn't currently catch situations where the sentence size is smaller than the biggest word, for example). Have a play and refine as required....

 

package routines;

 

public class WholeWordSplit {

 

 

  public static String[] splitSentence(String sentence, int sentenceSize) {

   

   String[] returnVal;

   String[] vals = sentence.split(" ");

 

   int size = 0;

   java.util.ArrayList<String> columns = new java.util.ArrayList<String>();

 

   for(int i=0; i< vals.length; i++){

   if(columns.size()<1 || (size+vals[i].length()+1) > sentenceSize){

   columns.add(vals[i]);

   size = vals[i].length();

   }else{

   String tmpStr = columns.remove(columns.size()-1);

   tmpStr = tmpStr+" "+ vals[i];

   columns.add(tmpStr);

   size = tmpStr.length(); 

   

   }

   }

   

   returnVal = columns.toArray(new String[columns.size()]);

 

   return returnVal;

 

  }

}

View solution in original post

1 Reply
Anonymous
Not applicable

A routine like this will work. I've just knocked it up and it will need refining (it doesn't currently catch situations where the sentence size is smaller than the biggest word, for example). Have a play and refine as required....

 

package routines;

 

public class WholeWordSplit {

 

 

  public static String[] splitSentence(String sentence, int sentenceSize) {

   

   String[] returnVal;

   String[] vals = sentence.split(" ");

 

   int size = 0;

   java.util.ArrayList<String> columns = new java.util.ArrayList<String>();

 

   for(int i=0; i< vals.length; i++){

   if(columns.size()<1 || (size+vals[i].length()+1) > sentenceSize){

   columns.add(vals[i]);

   size = vals[i].length();

   }else{

   String tmpStr = columns.remove(columns.size()-1);

   tmpStr = tmpStr+" "+ vals[i];

   columns.add(tmpStr);

   size = tmpStr.length(); 

   

   }

   }

   

   returnVal = columns.toArray(new String[columns.size()]);

 

   return returnVal;

 

  }

}