Split string at point where character appears

1177
8
Jump to solution
10-15-2012 06:00 AM
JoeMeny
New Contributor
I have a large dataset in ArcGIS10 with numeric characters followed by an asterisk and then a series of either numbers or letters.  I would like to be able to create a new field with only the characters to the right of the asterisk.  The left() and right() functions by themselves don't work for me, because there is no standard number of characters before or after the asterisk.

Example of data...

201*H
1296*CH
37901*001

Would like to create a new field with...

H
CH
001

Thanks for your help,
Joe
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
I have a large dataset in ArcGIS10 with numeric characters followed by an asterisk and then a series of either numbers or letters.  I would like to be able to create a new field with only the characters to the right of the asterisk.  The left() and right() functions by themselves don't work for me, because there is no standard number of characters before or after the asterisk.

Example of data...

201*H
1296*CH
37901*001

Would like to create a new field with...

H
CH
001

Thanks for your help,
Joe


Hi Joe,

You could use the field calculator to accomplish this.  Be sure to set the Parser to Python and try this:

!field_name!.split("*")[-1]


This will return only the values after the asterisk.  If you need to get everything before the asterisk, use [0] instead of [-1].  Also, be sure to replace field_name with the name of the field that you are wanting to split.

View solution in original post

0 Kudos
8 Replies
by Anonymous User
Not applicable
I have a large dataset in ArcGIS10 with numeric characters followed by an asterisk and then a series of either numbers or letters.  I would like to be able to create a new field with only the characters to the right of the asterisk.  The left() and right() functions by themselves don't work for me, because there is no standard number of characters before or after the asterisk.

Example of data...

201*H
1296*CH
37901*001

Would like to create a new field with...

H
CH
001

Thanks for your help,
Joe


Hi Joe,

You could use the field calculator to accomplish this.  Be sure to set the Parser to Python and try this:

!field_name!.split("*")[-1]


This will return only the values after the asterisk.  If you need to get everything before the asterisk, use [0] instead of [-1].  Also, be sure to replace field_name with the name of the field that you are wanting to split.
0 Kudos
JoeMeny
New Contributor
Thanks that worked, just what I was looking for.
0 Kudos
MelissaJohnson
Occasional Contributor II
I know this is an old thread but it sounds similar to what I have been looking for.  If I want to split a string and include the asterisk (in your example) how would I do that?  I have a string field and I am trying to calculate a new field with everything from a particular word to the end of the string.  Example:  Some Subdivision Sec 3; I want Sec 3 in a new field.

Thanks in advance for any assistance!
0 Kudos
by Anonymous User
Not applicable
Example: Some Subdivision Sec 3; I want Sec 3 in a new field.


Could you be a little more specific?  Could you show an example of the full value in one field and the value you want in your new field?

Are you saying the full field value is:

Greenview Estates Subdivision Sec 3;

and you just want just "Sec 3" in the other field?
0 Kudos
MelissaJohnson
Occasional Contributor II
Yes.  I have a subdivision name field.  Some are just single part subdivisions; eg Lakewood.  Others may have sections for example Lakeview Sec 1, Lakeview Sec 2, Lakeview Sec 3.  What I want to do is calculate a new field so that it has only the Section in it, for example Sec 1, Sec 2.  So ultimately one field will have the subdivision name and one field will have the section number in it.  Hope this makes more sense.  Some may not have any Section number as in the example of Lakewood above.
0 Kudos
by Anonymous User
Not applicable
If ALL subdivisions end with "Sec X", you can split the string by spaces and grab only the last 2 words:

>>> value = 'Lakeview Sec 2'
>>> section = ' '.join(value.split()[-2:])
>>> print section
Sec 2
>>> 


So in the Field Calculator "Section" field would be (Be sure to set the parser to Python):
" ".join(!Subdivision!.split()[-2:])


EDIT:  Just a thought, if you have a Sections feature class you can do a spatial join of your sections to your subdivisions.
0 Kudos
MelissaJohnson
Occasional Contributor II
Thanks for replying, no there is no guarantee that it is only 2 words past the word Sec.  Some say Sec 3 Rplt or could be even longer if it is a replat of certain lots in the description.  I do not have a separate Sections feature. 

The only thing I have thought of to do so far, and it seems like a strange way to do it, is to query from my Subdivision names LIKE '%SEC%'. then calculate the filtered results my new field using your code from earlier as such !SUBDIVNAME!.split("SEC")[-1].  Then I will have to calculate a new field with "SEC " & [SEC] my "temporary" field.  I was just hoping there was a way to get the SEC word over without the extra step.
0 Kudos
MelissaJohnson
Occasional Contributor II
I think I have figured out a way to do it.  It is not very elegant but I think it should save me some work.  Using your code above !field_name!.split("*")[-1], I can Select by Attributes anything with SEC to filter only those records I am interested in, then use your code to calculate a new field as:

"SEC" + !SUBDIVNAME!.split("SEC")[-1]

That way I don't have to do an extra step.

Thank you for your help!  I haven't used Python much.
0 Kudos