JavaScript to find the first non-repeated character in the string, javascript string

Source: Internet
Author: User

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...


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.