Select to view content in your preferred language

String from index position

1435
18
03-03-2014 12:18 PM
NoahHuntington
Deactivated User
Trying to calculate field using all the leftmost characters of [TEXTSTRING] up to the first occurrence of "out" in [TEXTSTRING] whose position has been calculated and placed in [POSITION] field.  The following is  vbscript but I'm sure there is an easy python solution to this. The script should help clarify...

[position] = first occurrence of "out" in [TEXTSTRING]

label:
Left( [TEXTSTRING], Len( [TEXTSTRING] ) [position] )


I hope this make sense?
Tags (2)
0 Kudos
18 Replies
MathewCoyle
Honored Contributor
If using field calculator try this.

!TEXTSTRING![:int(!position!)]


int() may or may not be required depending on field type of "position"
0 Kudos
by Anonymous User
Not applicable
Do you mean splitting it up by white space and finding where the word 'out' is within a string?  If so, you could get the position with something like this:

>>> a = 'this is out at pos 2'
>>> b = 'out at pos 0'
>>> c = 'pos 3 is out'
>>> def findIndex(string, word):
 return string.split().index(word)

>>> for test in [a, b, c]:
 print findIndex(test, 'out')

 
2
0
3


I don't know much VB so I'm not sure what you're doing once you have the index?

EDIT:  Matt's answer is waaaay better than mine.
0 Kudos
NoahHuntington
Deactivated User
If using field calculator try this.

!TEXTSTRING![:int(!position!)]


int() may or may not be required depending on field type of "position"


I have tried both of the following?

!TEXTSTRING![: !postion!]

!TEXTSTRING![: int(!postion!)]
0 Kudos
by Anonymous User
Not applicable
If you are just looking to get everything before the word "out" in a text string, how about:

!TEXTSTRING!.split("out")[0].strip()
0 Kudos
NoahHuntington
Deactivated User
Do you mean splitting it up by white space and finding where the word 'out' is within a string?  If so, you could get the position with something like this:

>>> a = 'this is out at pos 2'
>>> b = 'out at pos 0'
>>> c = 'pos 3 is out'
>>> def findIndex(string, word):
 return string.split().index(word)

>>> for test in [a, b, c]:
 print findIndex(test, 'out')

 
2
0
3


I don't know much VB so I'm not sure what you're doing once you have the index?

EDIT:  Matt's answer is waaaay better than mine.


No problem. Yeah I'm not sure what your testing in this one?  Thanks for chiming in Caleb!
0 Kudos
NoahHuntington
Deactivated User
If you are just looking to get everything before the word "out" in a text string, how about:

!TEXTSTRING!.split("out")[0].strip()


No luck here?
0 Kudos
MathewCoyle
Honored Contributor
Could you post a sample of your table you are trying to calculate and the applicable field types?
0 Kudos
NoahHuntington
Deactivated User
Could you post a sample of your table you are trying to calculate and the applicable field types?

Hi Matt

See attached:

label using textstring only...

label does not all show in table fyi.
0 Kudos
MathewCoyle
Honored Contributor
Are you trying to calculate a field or create a label?
0 Kudos