Solved

how to check case insensitive substring

Posted on 2016-09-01
5
46 Views
Last Modified: 2016-09-03
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
Comment
Question by:XTO
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:pepr
ID: 41780578
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
 

Author Comment

by:XTO
ID: 41780730
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
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 41781071
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
 

Author Comment

by:XTO
ID: 41781913
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
 
LVL 28

Expert Comment

by:pepr
ID: 41782987
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now