Solved

how to check case insensitive substring

Posted on 2016-09-01
5
70 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

733 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