Double Quote in Label Expression

6036
20
Jump to solution
12-18-2014 07:00 AM
m_neer
by
Occasional Contributor

Hello,

Trying to get my label expression to work here.  The problem is some of my table

records have a double quote in it (" symbol).  I want to rename as such see example below:

(30" PVC Pipe Drain)  and rename it to (30 inch)

I use the following vbScript - but get nowhere:

Function FindLabel ( [NAME] )

If Not IsNull([NAME]) Then

NewStr = Replace([NAME], "30'' PVC Pipe Drain", "30 inch")

  FindLabel = NewStr

End If

End Function

  --- also I tried python and get nowhere:

def FindLabel ( [NAME] 😞

  S = [NAME]

  S = S.replace( '30'' Drain, Vitreous Clay Pipe', '30 in')

  return S

0 Kudos
1 Solution

Accepted Solutions
TedKowal
Occasional Contributor III

Using VBScript....

Replace( [Name],chr(34)," inch")

No advance script or block needed.  --- Tested it with null values no issues..... if you simply want to replace " --> inch

Re reading you question If you want to shorten your label then you would need an advanced expression:

Function FindLabel ( [Name]   )
dblQuote = instr( [Name] ,chr(34))
if dblQuote > 0 then
   newStr = left( [Name] ,dblQuote - 1) & " inch"
else
   newStr = [Name]
end if
  FindLabel = newStr
End Function

View solution in original post

0 Kudos
20 Replies
Miguel_AngelBarrientos_Martíne
New Contributor II

Hi:

Can you replace temporary the " for another sign for example ºº and then replace again?

0 Kudos
m_neer
by
Occasional Contributor

no

0 Kudos
JamesFitzgerald
Occasional Contributor II

Hello M. Neer,

As a quick fix, you could export the attribute table to a databasefile.dbf and work on it from a standalone table to remove the quotation ("). then you could bring it back into arcGIS and join it to your shapefile.shp. Not as quick as code but it works!

Thanks,

James

0 Kudos
m_neer
by
Occasional Contributor

Yes, that is a valid idea but I had forgotten to mention
earlier that

this is a source feature  from our enterprise and I don't wish to

copy or manipulate the data in any way.   My
intention is to re-label

on the fly dynamically.
Copying the data and pointing to copied source

is what I am attempting to avoid here.

0 Kudos
JamesFitzgerald
Occasional Contributor II

Hey,

Replace 'St' or 'St.' starting a new word at the end of the string with the word 'Street'.

Expression: update_street(!ADDRESS!) 
 
Expression Type: PYTHON_9.3 
 
Code Block: 
import re 
def update_street(street_name): 
     return re.sub(r"""\b(St|St.)\Z""", 
               'Street', street_name) 

Check ArcGIS Desktop under Regular Expressions. 
0 Kudos
m_neer
by
Occasional Contributor

So, I attempted to reference my first posting above as an example (30" PVC Pipe Drain),  in correlation with your posted py parser and it didn't appear to do anything.

Of course I may not be applying it correctly? It runs but does not change anything at this point.

import re

def FindLabel([NAME]):

     return re.sub(r"""\b(30'' PVC Pipe Drain|30''Drain.)\Z""",

               '30 in', [NAME])

Other helpful info maybe?  Field Name = [NAME] and the text string in question  30'' PVC Pipe Drain

0 Kudos
TedKowal
Occasional Contributor III

Using VBScript....

Replace( [Name],chr(34)," inch")

No advance script or block needed.  --- Tested it with null values no issues..... if you simply want to replace " --> inch

Re reading you question If you want to shorten your label then you would need an advanced expression:

Function FindLabel ( [Name]   )
dblQuote = instr( [Name] ,chr(34))
if dblQuote > 0 then
   newStr = left( [Name] ,dblQuote - 1) & " inch"
else
   newStr = [Name]
end if
  FindLabel = newStr
End Function
0 Kudos
m_neer
by
Occasional Contributor

Excellent !   So very close.  Need to drop all the text AFTER the word inch.  When I attempt to

use LEFT() it reverts back to " double quote so I know we are close here.

Function FindLabel ( [NAME] )

If Not IsNull([NAME]) Then

NewStr = Replace([Name],chr(34)," inch")

  FindLabel = NewStr

End If

End Function

Was (30" PVC Pipe Drain) and now is (30 inch PVC Pipe Drain30" PVC Pipe Drain) just need to drop text

PVC Pipe Drain.   Perhaps compound it with LEFT()?

RichardFairhurst
MVP Honored Contributor

You should have included that detail in your description of what you were trying to do, since dropping text is a completely different issue.  So Try:

Function FindLabel ( [NAME] )

  If Not IsNull([NAME]) Then

    NewStr = [Name]

    If Instr(NewStr, chr(34)) > 0 Then

      NewStr = Left(NewStr, Instr(NewStr, chr(34)))

    End If

    NewStr = Replace(NewStr, chr(34), " inch")

    FindLabel = NewStr

  End If

End Function