Check if string contains Substring
Here given code implementation process.
/*
Java Program for
Check if string contains Substring
*/
public class Substring
{
public static void main(String[] args)
{
String text = "This is good algorithms to solve this problem";
// Test A
String value = "it";
if (text.contains(value))
{
System.out.print(value + " exists\n");
}
else
{
System.out.print(value + " not exists\n");
}
// Test B
value = "algos";
if (text.contains(value))
{
System.out.print(value + " exists\n");
}
else
{
System.out.print(value + " not exists\n");
}
// Test C
value = "solve";
if (text.contains(value))
{
System.out.print(value + " exists\n");
}
else
{
System.out.print(value + " not exists\n");
}
}
}
Output
it exists
algos not exists
solve exists
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ Program for
Check if string contains Substring
*/
int main()
{
string text = "This is good algorithms to solve this problem";
// Test A
string value = "it";
if (text.find(value) != string::npos)
{
cout << value << " exists\n";
}
else
{
cout << value << " not exists\n";
}
// Test B
value = "algos";
if (text.find(value) != string::npos)
{
cout << value << " exists\n";
}
else
{
cout << value << " not exists\n";
}
// Test C
value = "solve";
if (text.find(value) != string::npos)
{
cout << value << " exists\n";
}
else
{
cout << value << " not exists\n";
}
return 0;
}
Output
it exists
algos not exists
solve exists
// Include namespace system
using System;
/*
Csharp Program for
Check if string contains Substring
*/
public class Substring
{
public static void Main(String[] args)
{
String text = "This is good algorithms to solve this problem";
// Test A
String value = "it";
if (text.Contains(value))
{
Console.Write(value + " exists\n");
}
else
{
Console.Write(value + " not exists\n");
}
// Test B
value = "algos";
if (text.Contains(value))
{
Console.Write(value + " exists\n");
}
else
{
Console.Write(value + " not exists\n");
}
// Test C
value = "solve";
if (text.Contains(value))
{
Console.Write(value + " exists\n");
}
else
{
Console.Write(value + " not exists\n");
}
}
}
Output
it exists
algos not exists
solve exists
package main
import "fmt"
import "strings"
/*
Go Program for
Check if string contains Substring
*/
func main() {
var text string = "This is good algorithms to solve this problem"
// Test A
var value string = "it"
if strings.Contains(text, value) {
fmt.Print(value, " exists\n")
} else {
fmt.Print(value, " not exists\n")
}
// Test B
value = "algos"
if strings.Contains(text, value) {
fmt.Print(value, " exists\n")
} else {
fmt.Print(value, " not exists\n")
}
// Test C
value = "solve"
if strings.Contains(text, value) {
fmt.Print(value, " exists\n")
} else {
fmt.Print(value, " not exists\n")
}
}
Output
it exists
algos not exists
solve exists
<?php
/*
Php Program for
Check if string contains Substring
*/
function main()
{
$text = "This is good algorithms to solve this problem";
// Test A
$value = "it";
if (strpos($text, $value) !== false)
{
echo($value." exists\n");
}
else
{
echo($value." not exists\n");
}
// Test B
$value = "algos";
if (strpos($text, $value) !== false)
{
echo($value." exists\n");
}
else
{
echo($value." not exists\n");
}
// Test C
$value = "solve";
if (strpos($text, $value) !== false)
{
echo($value." exists\n");
}
else
{
echo($value." not exists\n");
}
}
main();
Output
it exists
algos not exists
solve exists
/*
Node JS Program for
Check if string contains Substring
*/
function main()
{
var text = "This is good algorithms to solve this problem";
// Test A
var value = "it";
if (text.includes(value))
{
process.stdout.write(value + " exists\n");
}
else
{
process.stdout.write(value + " not exists\n");
}
// Test B
value = "algos";
if (text.includes(value))
{
process.stdout.write(value + " exists\n");
}
else
{
process.stdout.write(value + " not exists\n");
}
// Test C
value = "solve";
if (text.includes(value))
{
process.stdout.write(value + " exists\n");
}
else
{
process.stdout.write(value + " not exists\n");
}
}
main();
Output
it exists
algos not exists
solve exists
# Python 3 Program for
# Check if string contains Substring
def main() :
text = "This is good algorithms to solve this problem"
# Test A
value = "it"
if (value in text) :
print(value ," exists")
else :
print(value ," not exists")
# Test B
value = "algos"
if (value in text) :
print(value ," exists")
else :
print(value ," not exists")
# Test C
value = "solve"
if (value in text) :
print(value ," exists")
else :
print(value ," not exists")
if __name__ == "__main__": main()
Output
it exists
algos not exists
solve exists
# Ruby Program for
# Check if string contains Substring
class Substring end
def main()
text = "This is good algorithms to solve this problem"
# Test A
value = "it"
if (text.include? value)
print(value ," exists\n")
else
print(value ," not exists\n")
end
# Test B
value = "algos"
if (text.include? value)
print(value ," exists\n")
else
print(value ," not exists\n")
end
# Test C
value = "solve"
if (text.include? value)
print(value ," exists\n")
else
print(value ," not exists\n")
end
end
main()
Output
it exists
algos not exists
solve exists
/*
Scala Program for
Check if string contains Substring
*/
class Substring()
{}
object Main
{
def main(args: Array[String]): Unit = {
var text: String = "This is good algorithms to solve this problem";
// Test A
var value: String = "it";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
// Test B
value = "algos";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
// Test C
value = "solve";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
}
}
Output
it exists
algos not exists
solve exists
import Foundation
/*
Swift 4 Program for
Check if string contains Substring
*/
func main()
{
let text: String = "This is good algorithms to solve this problem";
// Test A
var value: String = "it";
if (text.contains(value))
{
print(value ," exists");
}
else
{
print(value ," not exists");
}
// Test B
value = "algos";
if (text.contains(value))
{
print(value ," exists");
}
else
{
print(value ," not exists");
}
// Test C
value = "solve";
if (text.contains(value))
{
print(value ," exists");
}
else
{
print(value ," not exists");
}
}
main();
Output
it exists
algos not exists
solve exists
/*
Kotlin Program for
Check if string contains Substring
*/
fun main(args: Array < String > ): Unit
{
val text: String = "This is good algorithms to solve this problem";
// Test A
var value: String = "it";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
// Test B
value = "algos";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
// Test C
value = "solve";
if (text.contains(value))
{
print(value + " exists\n");
}
else
{
print(value + " not exists\n");
}
}
Output
it exists
algos not exists
solve exists
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment