JavaScript to find the first non-repeated character in the string, javascript string
This algorithm is for your reference only. You can only express it with the simplest idea.
// Find the first non-repeated character in the string // firstUniqueChar ("vdctdvc"); --> tfunction firstUniqueChar (str) {var str = str | "", I = 0, k = "", _ char = "", charMap = {}, result = {name: "", index: str. length}; for (I = 0; I <str. length; I ++) {_ char = str. charAt (I); if (charMap [_ char]! = Undefined) {charMap [_ char] =-1 ;}else {charMap [_ char] = I ;}for (k in charMap) {if (charMap [k] <0) {continue;} if (result. index> charMap [k]) {result. index = charMap [k]; result. name = k ;}} return result. name ;}
How to identify repeated numbers in JavaScript strings
No cycle is required. use regular expressions!
<Script>
Var s = "123,456, 33,123 ,";
Alert (/(? : ^ |,) (. *,). *, \ 1/g. test (s); // true
</Script>
---------------------
/(? : ^ |,) (. *,). *, \ 1/g
(? : ^ |,) Indicates the beginning of the text or the symbol ','... '? : 'Indicates non-get match, only match, not stored for later use,
(. *,) In your string, it indicates '100, 'and so on...
. * Any character 0 or multiple times
, \ 1 indicates ', 123 in your string,' where \ 1 is the reference (. *,) matched content.
/(? : ^ |,) (. *,). *, \ 1/g is a direct regular expression, and g indicates global search.
The following test is the RegExp method RegExp. test (string). This method checks whether a mode exists in the string. If yes, true is returned; otherwise, false is returned.
I don't know. You should take a look at the regular expression tutorials ,,,,
Write a program in c ++: how to find the first non-repeated letter in the string, that is, the first letter that appears only once, for example:
# Include <iostream> using namespace std; void f (char * s) {int cnt, I, l; char * s1; l = strlen (s ); for (I = 0; I <l; I ++) {s1 = s; cnt = 0; while (* s1) {if (* s1 = s [I]) {cnt ++; if (cnt> 1) break;} s1 ++;} if (cnt = 1) {cout <s <": "<s [I] <endl; return ;}} cout <s <": "<" not found "<endl ;}int main () {f ("addbccadfeg"); f ("addbccabfeg"); f ("aabbccdd"); return 0;} running result:
Addbccadfeg: baddbccabfeg: faabbccdd: Not found. Please press any key to continue...