d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Split A String With Delimiter
Add Reply New Topic New Poll
Member
Posts: 21,362
Joined: Sep 16 2009
Gold: 13.13
Jul 16 2012 04:42am
simple code:

StringToSplit = "https://www.facebook.com/myprofile";
String delimiter = "\\\\"; //means: split on \
String[] splitted =StringToSplit.split(delimiter);


output:

https:
/
/www.facebook.com
/myprofile

why does it split on slash? WTF splitting on \ works like "AB\nC" .

This post was edited by Br0 on Jul 16 2012 04:43am
Member
Posts: 8,852
Joined: Oct 25 2006
Gold: 4,972.50
Jul 16 2012 06:21am
what exactly are you trying to do?

notice that if you want to split / you need to do something like: String test2[] = test.split("/");
and, if you need to split \ you need this: String test2[] = test.split("\\");
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Jul 16 2012 07:05am
Here's why you need "\\\\" to split on \

String.split() takes a regular expression as an argument. Backslash is a special character in both regular expressions and java strings. If you want to match a backslash in a regular expression, you must use \\, and if you want to have a backslash in a java string you must have "\\".

So when you say String delimiter = "\\\\"; you are declaring that delimiter is a string representing the regular expression \\, which is the regular expression you need to match a single backslash.
Member
Posts: 8,852
Joined: Oct 25 2006
Gold: 4,972.50
Jul 16 2012 07:18am
got it, but in his case, he has // and not \\

try this:
Code
public class Test {
   public static void main (String[] args) {
       String test = "https://www.facebook.com/myprofile";
       String test2[] = test.split("/");
       for (String x : test2) {
           System.out.println(x);
       }
       
   }
}


and the result will be:

Code
https:

www.facebook.com
myprofile


notice the blank space on index[1] of the array, because the double '/'

This post was edited by ZergOwnZ on Jul 16 2012 07:18am
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Jul 16 2012 07:34am
Code
import java.lang.*;

public class SplitTest {

   public static void main(String[] args) {
       String toSplit = "https://www.facebook.com/myprofile";
       String delimiter = "\\\\";
       String[] pieces = toSplit.split(delimiter);

       for (String s : pieces) {
           System.out.println(s);
       }

       System.out.println("-----");

       delimiter = "/";
       pieces = toSplit.split(delimiter);

       for (String s : pieces) {
           System.out.println(s);
       }
   }
}


prints out

Code
https://www.facebook.com/myprofile
-----
https:

www.facebook.com
myprofile


This post was edited by PumblesMumbles on Jul 16 2012 07:34am
Member
Posts: 8,852
Joined: Oct 25 2006
Gold: 4,972.50
Jul 16 2012 09:25am
i think the main question is what he's trying to do with that :huh:
Member
Posts: 51,682
Joined: Jul 27 2007
Gold: 0.90
Jul 16 2012 10:34am
Quote (ZergOwnZ @ 16 Jul 2012 10:25)
i think the main question is what he's trying to do with that  :huh:


This lol
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll