Solved

how to check case insensitive substring

Posted on 2016-09-01
5
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 29

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 29

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 29

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…

691 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