Announcement

Collapse
No announcement yet.

Add dynamic number of space after a string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add dynamic number of space after a string

    I have a json object that I need to format having each field as a specified location. e.g. URNo 10char, PatientLastnanme 25char. URNo can be 6 characters only so I need to pad spaces after it so PatientLastName will start on the 11th position. How can I do it?

  • #2
    I don’t understand your question, post an example.

    Without understanding your question, this is simple JavaScript string manipulation.
    Diridium Technologies, Inc.
    https://diridium.com

    Comment


    • #3
      Input:
      URNO = '123456'
      PatientLastName = 'Reyes'

      Output:
      URNo = '123456 '
      PatientLastName = 'Reyes '

      (it doesnt seem to follow)
      expected is URNo + 4spaces to make it 10 then PatientLastName to follow on the 11th position

      Comment


      • #4
        Please post an exact example. That is not a JSON object, that is a simple string. For example post the complete JSON object you were expecting to create.
        Diridium Technologies, Inc.
        https://diridium.com

        Comment


        • #5
          {"MRNO":"000000003","SURNAME":"Lopez","FIRST_NA ME" :"Editted Grace","MIDDEL_NAME":"Vizcayno","GENDER":"F","CELL _NUMBER":"+639985397297","EMAIL":"[email protected] .com","MARITAL_STATUS":"Married","CITY":"Valenzuel a","ADDRESS":"1281 B Rosairo West St Tamaraw Hills, Gen. T. De Leon","Title":"Mrs","Postcode":"1223","DateOfBirth ":"1974-09-29T00:00:00+00:00","PhoneHome":"+639985397297","Ph oneWork":"+639985397297","Sex":null,"State":"METRO MANILA","SiteId":null,"Indigenous":null,"RefPract" :null,"RefAdd":null,"RefAdd1":null,"RefPhone":null ,"CivilStatus":null,"InterpreterRequired":null, "De pendentsNumber":"1"}

          I have the above as an input. Then I need to create a text output file where
          MRNo = 10 characters
          Surname = 25 characters
          Firstname = 25 character
          Gender = 1 character and so on.


          Expected output :
          000000003 Lopez Editted Grace F................... and so on

          NOTE: The post automatically trims spaces so the presentation of my expected output doesn't look right. The expectation is to fill with spaces if the string is less than the required number of characters.

          Comment


          • #6
            // add spaces to the left
            org.apache.commons.lang.StringUtils.leftPad('<YOUR STRING>', <WANTED LENGTH>, ' ');
            // or to the right of a string
            org.apache.commons.lang.StringUtils.rightPad('<YOU R STRING>', <WANTED LENGTH>, ' ');

            (The last Argument is the fill charcter - a space in your case. This is probably hard to see)

            Comment


            • #7
              There's also string prototypes padStart() and padEnd() - padEnd(targetLength, padString)

              The default arg. is blank spaces, so maybe something like:
              Code:
              MRNo = msg.MRNO.padEnd(10)

              Comment

              Working...
              X