How to Check If a String Contains 2 of the Same Character

How to check if a String contains two same characters?

You may check if a dot appears more than once in a string with a simple method checking if the first index of the char is not equal to the index of the last char occurrence:

boolean containsTwoDots(String str) { 
return str.indexOf('.') != str.lastIndexOf('.');
}

Way to check that string contains two of the same characters?

Yes, you can get the solution in one line easily with the count method of a string:

>>> # I named it 'mystr' because it is a bad practice to name a variable 'str'
>>> # Doing so overrides the built-in
>>> mystr = "Hello! My name is Barney!"
>>> mystr.count("!")
2
>>> if mystr.count("!") == 2:
... print True
...
True
>>>
>>> # Just to explain further
>>> help(str.count)
Help on method_descriptor:

count(...)
S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.

>>>

How to Check if a String has the same characters in JavaScript?

let str = "Sumur Bandung";
let x = "Kecamatan Sumur Bandung";

function stringContains(parentString, childString) {
const parentStringSeparator = parentString.split(" ");
return childString
.split(" ")
.every((word) => parentStringSeparator.includes(word));
}

console.log(stringContains(x, str));

If I understand you correctly, this is what you're asking. Given a parent string separated by spaces, check if every word of a child string is in the parent string.

Edit: This function doesn't take in account word order and splits every string with spaces.

Edit2: If you're trying to ask whether a child string contains at least one word from a parent string, you should use some instead of every:

let str = "Sumur Bandung";
let x = "Kecamatan Sumur Bandung";

function stringContains(parentString, childString) {
const parentStringSeparator = parentString.split(" ");
return childString
.split(" ")
.some((word) => parentStringSeparator.includes(word));
}

console.log(stringContains(x, str));

how to check if 2 string have same characters or no? - python

This is a well-known problem with many possible solutions. Try this:

def procedures(txt1, txt2):
return sorted(txt1) == sorted(txt2)

check if a string contains same character in R

another possibility:

sapply(strsplit(df1$Countries, ""), function(x) all(x == x[1]))

Check if whole string contains same character

You could split on every character then count the array for unique values.

if(count(array_count_values(str_split('abaaaa'))) == 1) {
echo 'True';
} else {
echo 'false';
}

Demo: https://eval.in/760293

Swift – How to find out if a string contains several identical characters?

You can use filter(_:) on the string and count to get the number of dots:

let str = "3..14"

switch str.filter({ $0 == "." }).count {
case 0:
print("string has no dots")
case 1:
print("string has 1 dot")
default:
print("string has 2 or more dots")
}

How to check if two strings contain same characters in Javascript?

You could merge the two strings then sort it then loop through it and if you find a match you could then exit out the loop.

I found this suggestion on a different stack overflow conversation:

var str="paraven4sr";
var hasDuplicates = (/([a-zA-Z]).*?\1/).test(str)

So if you merge the strings together, you can do the above to use a regexp, instead of looping.

check if string contains same characters twice

If you want to check for any word to be used twice, use the Split function to make a string into words, and then Group to get counts:

string input = "MyString MyString";
var words = input.Split().GroupBy(s => s).ToDictionary(
g => g.Key,
g => g.Count()
);

The dictionary words will be a set of key and value pairs where the key is the word, and the value is the number of times its in your input string. If you want to find words which occur more than once:

bool hasDuplicateWords = words.Any(w => w.Value > 1);

To find which words occur multiple times:

var duplicateWords = words.Where(w => w.Value > 1);

Edit: After editting your question, it seems you are not parsing simple strings, but parsing XML code. You should use an XML parser to work with XML, something like this (not checked in editor):

var input = "<Item> MyString <Item> <Item> MyString <Item>";
var xml = XElement.Parse(input);

bool hasDuplicateWords = xml.Children
.GroupBy(x => x.Name)
.Any(x => x.Count() > 1);


Related Topics



Leave a reply



Submit