英文:
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
上使用split
、slice
(从索引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 = "hi_my name is_reza";
const d = str.split("_").slice(1).map(e =>e[0]);
console.log(d);
<!-- end snippet -->
答案2
得分: 1
以下是翻译好的内容:
要获取 _
下划线 后的第一个字符(或字符),您可以使用以下代码:
const str = "hi_my name is_reza";
console.log(str.match(/(?<=_)./g))
或者使用 /(?<=_)./g
要更改 _
后的字符(以便将其大写)并一次性删除下划线,您可以使用 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 = "hi_my name is_reza";
console.log(str.match(/(?<=_)./g))
<!-- end snippet -->
or with /(?<=_)[^_]/g
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 = "hi_my name is_reza";
const output = str.replace(/_(.)/g, (m, m1) => ` ${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 = '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 _
<!-- 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 = "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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论