how to find whether a string is contained in another string

check if string contains substring python
python check if string contains substring from list
check if string contains substring java
check if a string is a substring of another string in c
check if string contains substring javascript
python check if string contains only certain characters
check if string contains substring php
python check if url contains string
a='1234;5'
print a.index('s')

the error is :

> "D:\Python25\pythonw.exe"  "D:\zjm_code\kml\a.py" 
Traceback (most recent call last):
  File "D:\zjm_code\kml\a.py", line 4, in <module>
    print a.index('s')
ValueError: substring not found

thanks

Try using find() instead - this will tell you where it is in the string:

a = '1234;5'
index = a.find('s')
if index == -1:
    print "Not found."
else:
    print "Found at index", index

If you just want to know whether the string is in there, you can use in:

>>> print 's' in a
False
>>> print 's' not in a
True

How to Check if a Python String Contains Another String?, Given two strings s1 and s2, find if s1 is substring of s2. If yes, return index of first occurrence, else return -1. Examples : Input : s1 = "for", s2 = "geeksforgeeks"  Java – Check if a String contains a substring. In Java, we can use String.contains() to check if a String contains a substring. 1. String.contains() – Case Sensitive. 2. String.contains() – Ignoring Case. 3. indexOf() 3.1 Review the JDK String source code, the contains() uses indexOf() to check substring internally.

print ('s' in a)     # False
print ('1234' in a)  # True

Use find if you need the index as well, but don't want an exception be raised.

print a.find('s')    # -1
print a.find('1234') # 0

Check if a string is substring of another, 1. Use find. The find method checks if the string contains a substring. If it does, the method returns the starting index of  Check if the given string is shuffled substring of another string; Construct an Array of size N in which sum of odd elements is equal to sum of even elements; Split the binary string into substrings with equal number of 0s and 1s; Find the Nth Pure number; Count all sub-strings with weight of characters atmost K; Split the given string into Odds: Digit DP

you can use in operator if you just want to check whether a substring is in a string.

if "s" in mystring:
   print "do something"

otherwise, you can use find() and check for -1 (not found) or using index()

How to check if Python string contains another string, Try using find() instead - this will tell you where it is in the string: a = '1234;5' index = a.find('s') if index == -1: print "Not found." else: print "Found  In excel, substring is a part of another string. It can be a single character or a whole paragraph. To search a string for a specific substring, we will use the ISNUMBER function along with the FIND function in Excel. Instead of FIND, you can always use the excel SEARCH function for non-case sensitive searches.

str.find() and str.index() are nearly identical. the biggest difference is that when a string is not found, str.index() throws an error, like the one you got, while str.find() returns -1 as others' have posted.

there are 2 sister methods called str.rfind() and str.rindex() which start the search from the end of the string and work their way towards the beginning.

in addition, as others have already shown, the in operator (as well as not in) are perfectly valid as well.

finally, if you're trying to look for patterns within strings, you may consider regular expressions, although i think too many people use them when they're overkill. in other (famous) words, "now you have two problems."

that's it as far as all the info i have for now. however, if you are learning Python and/or learning programming, one highly useful exercise i give to my students is to try and build *find() and *index() in Python code yourself, or even in and not in (although as functions). you'll get good practice traversing through strings, and you'll have a better understanding as far as how the existing string methods work.

good luck!

how to find whether a string is contained in another string, In this article, we'll examine four ways to use Python to check whether a string contains a substring. Each has their own use-cases and pros/cons, some of which  @stefan ,you are right,there is a find method,but what about split,replace and many other staff.You can compare std::string to the string api in Java.PS:Also I do think contains is much more elegant than find to check if a string contains another string. – Geng Jiawen Jun 23 '14 at 8:01

This is the correct way of checking whether a substring is present in a string or not.

def sea(str,str1):

if(str1[0:] in str):

    return "yes"
else:
    return "no"
print(sea("sahil","ah"))

Python: Check if String Contains Substring, String.contains() – Case Sensitive. JavaExample1.java. package com.mkyong; public class JavaExample1 { public static void main(String[]  So, now we know where the name came from and what it's supposed to do. Lets get back to Regular Expressions. The arguments, even though they are strings, they are used to build regular expressions (henceforth: regex). A regex is a way to match a string (if this definition irritates you, let it go).

Java – Check if a String contains a substring – Mkyong.com, The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if  You can use the in operator or the string’s find method to check if a string contains another string. The in operator returns True if the substring exists in the string. Otherwise, it returns False. The find method returns the index of the beginning of the substring if found, otherwise -1 is returned.

String contains() Method in Java with Example, If a sub-string was found, a pointer to its first occurrence is returned. You can use suffix array and binary_search to find whether a string is substring of another​  We can also use the built in .NET string extension function Contains to check whether a string contains a another string. $strVal ='Hello world' if($strVal.Contains('world')) { Write-Host 'True' } else { Write-Host 'False' } The .Contains() function is case sensitive.

How to check whether one string is a sub-string of another string in , <script>; // Sample string; var str = "The quick brown fox jumps over the lazy dog." // Check if the substring exists inside the string; var index = str.indexOf("fox"); What is contains()method in Java? The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. Syntax of String "Contain" method public boolean String.contains(CharSequence s) Parameters. s − This is the sequence to search . Return Value

Comments
  • This is a duplicate of another question: stackoverflow.com/questions/3437059/…
  • a string in another string ? you said it. this is python
  • @Anderson: agree, It should be closed as duplicated.
  • LOL, awesome, I was redirected here from another entry :P
  • Regular Expressions a Overkill?! Why? There's probably no more efficient and clear way of programming most string matching algorithms.
  • For simple searches that can be done with tools like find() and index(), regular expressions are not as good a choice because they tend to be difficult to read without careful inspection. It's rare to be able to skim past a regexp and catch all the details of what it's trying to do. They can be very efficient for complex patterns but maintainability can be an issue.