Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

how to check case insensitive substring

I get a "string indices must be integers" error when I try either of the following,

 
partialUpper = workspacePath_base[firstIndex, UNC_Path.__len__()]

Open in new window


or

 
partialUpper = workspacePath_base[firstIndex, len(UNC_Path)]

Open in new window


Here is the original context that got me trying to figure this out,

 if UNC_Path.upper() in workspacePath_base[firstIndex, len(UNC_Path)].upper() :  

Open in new window


I know that I need to do something like

 if UNC_Path.upper() in workspacePath_base[0, 9].upper() :  

Open in new window


But that doesn't allow me to check dynamically.

This is Python 2.7
Thanks.
0
XTO
Asked:
XTO
  • 3
  • 2
1 Solution
 
peprCommented:
Is it copied from a working code? Looking closer, don't you see something like...
if UNC_Path.upper() in workspacePath_base[firstIndex:len(UNC_Path)].upper():

Open in new window

...that is colon instead of coma inside the square brackets?

I guess that the workspacePath_base is of string type. If comma is used inside square brackets, it is converted to a tuple. You could rewrite it more explicitly as...
if UNC_Path.upper() in workspacePath_base[(firstIndex, len(UNC_Path))].upper():

Open in new window

(notice the extra parentheses). Then a string object would actualy want to say that you cannot use a tuple as an index.

On the other hand, the syntax like s[i1:i2] is called slicing in Python. For string objects, it says that you get substring of characters from index i1 (including), to index i2 (excluded).
0
 
XTOAuthor Commented:
It is an attempt at slicing. I want to see if the upper case version of the string, UNC_Path is contained within some segment of workspacePath_base.

I had also tried the simpler,
 if UNC_Path.upper() in workspacePath_base.upper()  :

Open in new window


But the debugger just stepped past it when there was a matching substring.

The end goal is to check to see if one substring is contained within the longer string, regardless of case.
0
 
peprCommented:
Do print(repr(UNC_Path)) and the same for workspacePath_base just before the if to see if the strings really contain the values you expect. (Simply said, the repr() just adds the quotes around the string.) Do the same with the values changed to .upper().

For the UNC, you may want to use the .startswith() method instead of the in operator.
0
 
XTOAuthor Commented:
The .startswith() method worked and did the job.

Both of these worked:

        if workspacePath_base.startswith(NAS01a_Proj_Arcview) :

Open in new window

       

        if workspacePath_base.upper().startswith(NAS01a_Proj_Arcview.upper() ) :

Open in new window

0
 
peprCommented:
Actually, the .startswith() does not do the case-insensitive testing. For the paths, you may want to use os.path.normcase(). You may also be interested in os.path.splitdrive(). You may find also other methods (in the documentation) that may be nice to use in your case.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now