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
zinzinzan
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
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.

Please use plain text.
Developer
peter_strange
Posts: 19,607
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. 

Please use plain text.
Developer
moniko17
Posts: 216
Registered: ‎08-23-2012
My Device: Javelin 8900

Re: Remove breakline from String

maybe u can use function replace

 

Press the Accept as solution Button when u got the Solution

Please use plain text.
Developer
zinzinzan
Posts: 90
Registered: ‎04-03-2012
My Device: 9100

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

Please use plain text.
Developer
peter_strange
Posts: 19,607
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. 

Please use plain text.
Developer
zinzinzan
Posts: 90
Registered: ‎04-03-2012
My Device: 9100

Re: Remove breakline from String

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

Please use plain text.
Developer
peter_strange
Posts: 19,607
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....

Please use plain text.
Developer
anditsung
Posts: 246
Registered: ‎09-30-2010
My Device: Z10, Onyx2

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?
Please use plain text.
Developer
peter_strange
Posts: 19,607
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.

Please use plain text.
Developer
pankajace12
Posts: 206
Registered: ‎04-30-2011
My Device: BlackBerry Z10

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

Please use plain text.