即如果我有一个输入字符串:
input ='你好世界,怎么了?'
我想要一个输出字符串:
desiredOutput ='Hello World,whats up?'
如果字符串中任何单词的第一个字母已经是大写字母,请保持原样。
答案 0 :(得分:8)
function upperCaseFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lowerCaseAllWordsExceptFirstLetters(string) {
return string.replace(/\w\S*/g, function (word) {
return word.charAt(0) + word.slice(1).toLowerCase();
});
}
var input = 'hello World, whatS up?';
var desiredOutput = upperCaseFirstLetter(lowerCaseAllWordsExceptFirstLetters(input));
console.log(desiredOutput);
基于:
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
和
How do I make the first letter of a string uppercase in JavaScript?
答案 1 :(得分:3)
我试图做同样的事情,但发现了一种更简单的方法:
function formatString(str) {
return str
.replace(/(\B)[^ ]*/g,match =>(match.toLowerCase()))
.replace(/^[^ ]/g,match=>(match.toUpperCase()));
}
var text = "aaa BBB CCC";
console.log(formatString(text));
答案 2 :(得分:2)
using (var stream = model.ImageFile.OpenReadStream())
{
Image originalImage = Image.FromStream(stream, true, true);
//Create the cropbox
Rectangle cropbox = new Rectangle(-5, -25, 400, 400) //these values are just examples, not constants
//Crop image (won't work because cropbox is outside boundaries of the original image)
Bitmap croppedBitmap = new Bitmap(originalImage).Clone(cropbox, PixelFormat.DontCare);
}
答案 3 :(得分:0)
function titleCase(str) {
return str.split(' ').map(item =>
item.charAt(0).toUpperCase() + item.slice(1).toLowerCase()).join(' ');
}
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));
//prints "Here Is My Handle Here Is My Spout";
更改str
参数值,一切顺利。