Gloripaxis
Gloripaxis

Reputation: 1016

How to split a string over a specific character?

I have a date in the form of a string, and it looks like this:

"21.07. - 10.08."

I was wondering how do I split the two into two strings, and lose the " - ".

Something that would make string1 = "21.07." and string2 = "10.08."

Upvotes: 0

Views: 942

Answers (3)

alexk
alexk

Reputation: 1564

I actually don't know Python, but I found this thread.

Seems like you can split into a list:

mylist = mystr.split("-", 1)

Then...

string1 = mylist[0]; #This contains "21.07."
string2 = mylist[1]; #This contains "10.08."

Upvotes: 0

John Brown
John Brown

Reputation: 189

Split the String at the "-". This will give you a String array. Concatenate the two array elements together to form one String.

In java this would be:

 String[] splitted = stringToSPlit.split("-");
 String result = splitted[0] + splitted[1];

Upvotes: 0

Jared
Jared

Reputation: 26397

string1, string2 = '21.07. - 10.08.'.split(' - ')

Upvotes: 1

Related Questions