| checkByIndexOfWithBoundary |
const testStr1 = "张三.1 , 李四.2,王五.3 ,赵六.4";
console.log(checkByIndexOfWithBoundary(testStr1, "李四.2"));
console.log(checkByIndexOfWithBoundary(testStr1, "王五.3"));
console.log(checkByIndexOfWithBoundary(testStr1, "赵六.4"));
console.log(checkByIndexOfWithBoundary(testStr1, "张三.0"));
console.log(checkByIndexOfWithBoundary(testStr1, "李四"));
function checkByIndexOfWithBoundary(commaStr, target) {
const targetLen = target.length;
const strLen = commaStr.length;
let pos = commaStr.indexOf(target);
if (pos === -1) return false;
while (pos !== -1) {
const prevOk = pos === 0 || /[, ]/.test(commaStr[pos - 1]);
const nextOk = (pos + targetLen) === strLen || /[, ]/.test(commaStr[pos + targetLen]);
if (prevOk && nextOk) return true;
pos = commaStr.indexOf(target, pos + 1);
}
return false;
}
| ready |
| checkByIndexOfWithSpace |
const testStr1 = "张三.1 , 李四.2,王五.3 ,赵六.4";
console.log(checkByIndexOfWithSpace(testStr1, "李四.2"));
console.log(checkByIndexOfWithSpace(testStr1, "王五.3"));
console.log(checkByIndexOfWithSpace(testStr1, "赵六.4"));
console.log(checkByIndexOfWithSpace(testStr1, "张三.0"));
console.log(checkByIndexOfWithSpace(testStr1, "李四"));
function checkByIndexOfWithSpace(commaStr, target) {
const trimmedStr = commaStr.replace(/\s*,\s*/g, ',').replace(/^\s+|\s+$/g, '');
const wrappedStr = `,${trimmedStr},`;
const wrappedTarget = `,${target.trim()},`;
return wrappedStr.indexOf(wrappedTarget) !== -1;
}
| ready |
| containsUser |
const testStr1 = "张三.1 , 李四.2,王五.3 ,赵六.4";
console.log(containsUser(testStr1, "李四.2"));
console.log(containsUser(testStr1, "王五.3"));
console.log(containsUser(testStr1, "赵六.4"));
console.log(containsUser(testStr1, "张三.0"));
console.log(containsUser(testStr1, "李四"));
function containsUser(commaStr, target) {
if (!commaStr) return false;
const userSet = new Set(commaStr.split(',').map(s => s.trim()));
return userSet.has(target);
}
| ready |