Posts Under 'String'

How to remove white-space from a String

/*** replace multiple whitespaces between words with single blank */ String before=" hi how are you "; String value = before.trim().replaceAll("[ ][ ]*", " "); or String value = before.replaceAll("\\b\\s{2,}\\b", " "); //before : hi how are you //after: hi how are you /* remove leading whitespace */ String value= before.replaceAll("^\\s+", ""); //before : hi […]

Read more »


Display specific length string without truncating word in Java

If you want to display substring of text and don’t want to truncate word in a substring, then you can use below method to fulfill your requirement. public static String textSubString(String text, int start, int end){ String str=""; int limit=0; limit=end; if(text.length()!=0 && text!=""){ String substr=text.substring(end,end+1); if(substr!=""){ while(!substr.equals("")){ substr=text.substring(limit,++limit); substr=substr.trim(); } } str=text.substring(start,limit); } return […]

Read more »


how to find carriage return in string using java

Just like you would identify any other character in a string: look for “\r\n” or “\n” (system dependent).

Read more »