在字符串中的每个大写字符之前添加一个空格(不使用正则表达式)

时间:2016-10-05 12:58:38

标签: javascript string

我有字符串,我试图在该字符串中的每个大写字母前添加一个空格。这就是我所拥有的

function spinalCase(str) {
  // check if string has no spaces
  if (str.indexOf(" ") == -1) {
    for (var j = 0; j < str.length-1; j++) {
      // find the capital letters
      if (str.charAt(j) == str.charAt(j).toUpperCase()) {
        // here is where I fail
        str = str.split(str.charAt(j)).join(' ');
      }
      else
        break;
    }

  return str;
}

spinalCase("thisIsSpinalTap");
/*

1 个答案:

答案 0 :(得分:1)

请使用var s = "thisIsSpinalTap"; s = s.replace(/([A-Z])/g, ' $1').trim(); console.log(s);在每个大写字母前添加一个空格。

&#13;
&#13;
Uri sourceBlobUri = new Uri("https://mystorage.blob.core.windows.net/a-container/a.json");
Uri targetBlobUri = new Uri("https://mystorage.blob.core.windows.net/a-container-archive/a.json");

var prodTokenSource = @"A_SAS_TOKEN_WITH_A_IP_RESTRICTION";
var prodTokenArchive = @"A_SAS_TOKEN_WITH_A_IP_RESTRICTION";

StorageCredentials sourceCredentials = new StorageCredentials(prodTokenSource);
StorageCredentials targetCredentials = new StorageCredentials(prodTokenArchive);

CloudBlockBlob sourceBlob = new CloudBlockBlob(sourceBlobUri, sourceCredentials);
CloudBlockBlob targetBlob = new CloudBlockBlob(targetBlobUri, targetCredentials);

await targetBlob.StartCopyAsync(sourceBlob); //Fails 403 error
&#13;
&#13;
&#13;