skip to Main Content

I have function

firstLetterUppercase(str: string) {
    let splitStr = str.toLowerCase().split(' ');
    for (let i = 0; i < splitStr.length; i++) {
      splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }
    return splitStr.join(' ');
  }

It’s work fine for string: Warszawa, Krakow, Bielsko – Biala etc.

I need change it to string ‘Bielsko-biala’.
(i need first char after ‘-‘ uppercase too).

How can i make it?

Please help me

4

Answers


  1. You can use a regular expression and the replace() method to find and replace the lowercase letters after a dash with their uppercase counterparts.

    function firstLetterUppercase(str: string) {
      let splitStr = str.toLowerCase().split(" ");
      for (let i = 0; i < splitStr.length; i++) {
        splitStr[i] =
          splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
        splitStr[i] = splitStr[i].replace(/-([a-z])/g, (match) =>
          match.toUpperCase()
        );
      }
      return splitStr.join(" ");
    }
    
    Login or Signup to reply.
  2. Here is the updated code that will work for ‘Bielsko-biala’.
    In the modified function, I have added a conditional statement to check if the current word contains a hyphen (‘-‘). If it does, I locate the index of the hyphen using indexOf(‘-‘). Then, it capitalizes the letter immediately after the hyphen by concatenating the relevant portions of the string with their capitalized counterparts.

    function firstLetterUppercase(str) {
      let splitStr = str.toLowerCase().split(' ');
    
      for (let i = 0; i < splitStr.length; i++) {
        if (splitStr[i].includes('-')) {
          let hyphenIndex = splitStr[i].indexOf('-');
          splitStr[i] =
            splitStr[i].charAt(0).toUpperCase() +
            splitStr[i].substring(1, hyphenIndex + 1) +
            splitStr[i].charAt(hyphenIndex + 1).toUpperCase() +
            splitStr[i].substring(hyphenIndex + 2);
        } else {
          splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
        }
      }
    
      return splitStr.join(' ');
    }
    
    console.log(firstLetterUppercase('Bielsko-biala'))
    Login or Signup to reply.
  3. If you want to transform your text into uppercase for every first letter of a word you can map your input after turning it into an array like this:

    function toUpperCase(str) {
      const spiltedArr = str.split('-');
      let arrParts = spiltedArr.map(function(item) {
        return item.charAt(0).toUpperCase() + spiltedArr.slice(1);
      });
      return arrParts .join('-');
    }
    

    Then just call it with some params:

    toUpperCase("first-asdasd")

    Dont forget to return string and not the splited array

    Login or Signup to reply.
  4. One way of doing it can be:

    function firstLetterUppercase(str) {
      let splitStr = str.toLowerCase().split(' ');
      for (let i = 0; i < splitStr.length; i++) {
        splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
      }
    
      //Added code
      let result = splitStr.join(' ');
      result = result.replace(/-([a-z])/g, function(match, group1) {
        return '-' + group1.toUpperCase();
      });
    
      return result;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search