Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet
Accepted Solution

Remove breakline from String

hi there i am trying to remove a breaks lines a String = http://goo.gl/IITlS\\n\\n this is the complete response from a url_shortener , i try lot of ways like indexOf("\n") , indexOf("\\n"), indexOf("\"), but just return -1 , that means not subString found.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Remove breakline from String

[ Edited ]

"\n" is the new line character, a single character. To get the character "\" followed by the character "n" you need to escape the String, i.e. \\n

 

Edit:

 

Apologies, did not notice you have already tried this...  Please ignore this post. 

Developer
Posts: 216
Registered: ‎08-23-2012
My Device: Javelin 8900
My Carrier: Axis

Re: Remove breakline from String

maybe u can use function replace

 

Press the Accept as solution Button when u got the Solution

Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Remove breakline from String

well i try this URL_SHORT = URL_DIRTY.substring(0,URL_DIRTY.indexOf("\\n")); and URL_DIRTY.indexOf("\\n") and returns -1 that means subString not found

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Remove breakline from String

Hm, something strange is going on here.

 

I suggest you debug, and then investigate the String you are trying to shorten and make sure you know exactly which characters are in there.  Printing the String out may not give you exactly what is there. 

Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Remove breakline from String

Well i used the debug, check this out.brkline.png

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Remove breakline from String

They really are line breaks.  so

indexOf("\n")

should find them!  Weird....

Developer
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2
My Carrier: IND XL

Re: Remove breakline from String

i try like this

String ab = "http://goo.gl/IITlS\\n\\n";
int a = ab.indexOf("\\n");
System.out.println(ab.substring(0, a));

works fine.
is there something wrong with your code?
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Remove breakline from String

I fyou look at the debug screen shot and note the output, you will see that the lines are not \\n, but are innfact actually line breaks, i.e. \n.

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: Remove breakline from String

Hi

 

Let try it.

 

package pk.jha;

public class Testing {
	
	public static void main(String args[])
	{
		
	String test = replaceAll("http://goo.gl/IITlS\\n\\n","\\","/");
	System.out.println("Test ::" + test);
	
	}
	public static String replaceAll(String source, String pattern, String replacement)
	{    

	    //If source is null then Stop
	    //and retutn empty String.
	    if (source == null)
	    {
	        return "";
	    }

	    StringBuffer sb = new StringBuffer();
	    //Intialize Index to -1
	    //to check agaist it later 
	    int idx = 0;
	    //Search source from 0 to first occurrence of pattern
	    //Set Idx equal to index at which pattern is found.

	    String workingSource = source;
	    
	    //Iterate for the Pattern till idx is not be -1.
	    while ((idx = workingSource.indexOf(pattern, idx)) != -1)
	    {
	        //append all the string in source till the pattern starts.
	        sb.append(workingSource.substring(0, idx));
	        //append replacement of the pattern.
	        sb.append(replacement);
	        //Append remaining string to the String Buffer.
	        sb.append(workingSource.substring(idx + pattern.length()));
	        
	        //Store the updated String and check again.
	        workingSource = sb.toString();
	        
	        //Reset the StringBuffer.
	        sb.delete(0, sb.length());
	        
	        //Move the index ahead.
	        idx += replacement.length();
	    }

	    return workingSource;
	}
}

 output is

 

Test ::http://goo.gl/IITlS/n/n

 

Thanks