找到`replaceAll()`函数后的第一个字母。

huangapple go评论66阅读模式
英文:

finding the first letters after the replaceAll() in j.s

问题

你可以使用以下方法来找到并访问在 replaceAll() 后面的第一个字母:

String input = "hi_my name is_reza";
String replaced = input.replaceAll("_", " ");
int index = replaced.indexOf(' ');

if (index != -1 && index + 1 < replaced.length()) {
    char letter = replaced.charAt(index + 1);
    // letter 现在包含了 "my" 中的字母 'm',以及 "reza" 中的字母 'r'
}

这个代码会将下划线替换为空格,然后找到第一个空格后的字母。

英文:

hi how can we find and access the first letters that comes right after the replaceAll() ?
like we have : hi_my name is_reza => replaceAll("_"," ") => so now i want to access to (m) from my and to (r) from reza

id did try split() and indexOf() method but it just aiming one of underscores not all of them

答案1

得分: 1

你可以在hi_my name is_reza上使用splitslice(从索引1开始到末尾,因为你不想要第一个'_'的左侧部分)和map(对数组的每个元素执行操作)如下所示:

const str = "hi_my name is_reza";
const d = str.split("_").slice(1).map(e => e[0]);

console.log(d);
英文:

You can use split, slice (start from index 1 to the end, because you don't want the left part of the first '_') and map (which execute over every element of an array) on hi_my name is_reza like so :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &quot;hi_my name is_reza&quot;;
const d = str.split(&quot;_&quot;).slice(1).map(e =&gt;e[0]);

console.log(d);

<!-- end snippet -->

答案2

得分: 1

以下是翻译好的内容:

要获取 _ 下划线 后的第一个字符(或字符),您可以使用以下代码:

const str = "hi_my name is_reza";
console.log(str.match(/(?<=_)./g))

或者使用 /(?<=_)./g

在Regex101.com上查看示例

要更改 _ 后的字符(以便将其大写)并一次性删除下划线,您可以使用 String.prototype.replace(),如下所示:

const str = "hi_my name is_reza";
const output = str.replace(/_(.)/g, (m, m1) => ` ${m1.toUpperCase()}`)
console.log(output)
英文:

The get the first character(s) after _ underscore, you could use:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &quot;hi_my name is_reza&quot;;
console.log(str.match(/(?&lt;=_)./g))

<!-- end snippet -->

or with /(?&lt;=_)[^_]/g

Demo on Regex101.com

To change that letter after _ (in order to capitalize it) and remove the underscore all in one go, you could use String.prototype.replace() like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &quot;hi_my name is_reza&quot;;
const output = str.replace(/_(.)/g, (m, m1) =&gt; ` ${m1.toUpperCase()}`)
console.log(output)

<!-- end snippet -->

答案3

得分: 1

以下是翻译好的部分:

"By this, you can do both replace and get those string after _"

let str = 'hi_my name is_reza';
let res = [];
const final = str.replaceAll(/_/g, function (r, index) {
    res.push(str[index + 1]);
    return ' ';
});

console.log(final, res); // by this you can replace and get the string after _

如果您需要进一步的翻译,请告诉我。

英文:

> By this, you can do both replace and get those string after _

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let str = &#39;hi_my name is_reza&#39;;
let res = [];
const final = str.replaceAll(/_/g,function(r,index){
  res.push(str[index+1]);
  return &#39; &#39;
});

console.log(final,res)// by this you can replace and get the string after _

<!-- end snippet -->

答案4

得分: 0

Thank you all. Actually, I was looking for a way to remove all underlines and replace the first letter after them to be capitalized. So I used your solution to achieve that.

const str = "hi_my name is_reza";
const d = str
  .split("_")
  .map((word, index) => {
    return index !== 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word;
  })
  .join(" ")
  .replaceAll("_", " ");

console.log(d);
英文:

thank you all . actually i was looking for a way to remove all underlines and replacing the first letter after them to be capital . so i used your solution to achieve that .

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const str = &quot;hi_my name is_reza&quot;;
const d = str
  .split(&quot;_&quot;)
  .map((word, index) =&gt; {
    return index !== 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word;
  })
  .join(&quot; &quot;)
  .replaceAll(&quot;_&quot;, &quot; &quot;);

console.log(d);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 23:15:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定