Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

split string

Explorer ,
Apr 11, 2021 Apr 11, 2021

Is there a way to export last word of a string into a new field?

TOPICS
How to , JavaScript
5.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
3 ACCEPTED SOLUTIONS
Community Expert ,
Apr 11, 2021 Apr 11, 2021

You can use this code as validation script of a field where text is:
this.getField("Text1").value = event.value.split(" ").pop();

Change field name if needed.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2021 Apr 11, 2021

Just add this '+event.value.split(" ").shift();' to the code like this:
this.getField("Text1").value = event.value.split(" ").pop()+event.value.split(" ").shift();
TIP: split(), splits string into array.
pop(), removes last element of array and returns that element
and shift(), removes first element of array and returns that element.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2021 Apr 21, 2021

Use this:

var str = this.getField("Text1").valueAsString;
var xstr = str.split("");
xstr.push(xstr.shift());
ystr = xstr.join("");
event.value = ystr;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2021 Apr 11, 2021

You can use this code as validation script of a field where text is:
this.getField("Text1").value = event.value.split(" ").pop();

Change field name if needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 11, 2021 Apr 11, 2021

Thank you so much. I have additional question.
In my string first and last word are numbers. How would I extract both numbers and then join them,
but numbers from end of the strings should be first?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2021 Apr 11, 2021

Just add this '+event.value.split(" ").shift();' to the code like this:
this.getField("Text1").value = event.value.split(" ").pop()+event.value.split(" ").shift();
TIP: split(), splits string into array.
pop(), removes last element of array and returns that element
and shift(), removes first element of array and returns that element.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2021 Apr 11, 2021

Do you want to add the numbers to each other, or join them as strings?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2021 Apr 12, 2021

As i wrote in my post, I wanted numbers from end of the string to be first for example "12 hello 34"

and I wanted numbers to be 3412, so Nesa code gives me that. Is there better solution?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2021 Apr 12, 2021

No, Nesa's solution is excellent. I just thought you might wanted to add those numbers together (so in your example the result would be 46), not concatenate them as strings. But if that's what you want to do then Nesa's solution should work just fine.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2021 Apr 11, 2021

This article explains it all:

https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings/

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2021 Apr 12, 2021

Thanks Thom Il take a look at it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 21, 2021 Apr 21, 2021

Hi, I have additional question. In my text field "Text1" I have a string that starts with a letter and end with a number.
I want to show this in second field but I need letter to be next to a number. e.g. original text: "X some text 1234"
new text: "some text 1234X".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2021 Apr 21, 2021

Use this:

var str = this.getField("Text1").valueAsString;
var xstr = str.split("");
xstr.push(xstr.shift());
ystr = xstr.join("");
event.value = ystr;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2022 Dec 11, 2022

I have a text field (text1) with a sentence of 8 words. I am trying to display each word in a different text fields from text2 to text 9 (1 word/ field). I am a beginner using PDF and Scripts, but I really appreciate detailed explanation to do that.

Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2022 Dec 11, 2022

You can use this (assuming all words are separated by space character) as validation script of "text1" field:

var str = event.value.split(" ");
for( var i=0; i<=7; i++){
this.getField("text"+(i+2)).value = str.length == 8 ? str[i] : "";}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2022 Dec 12, 2022
Hi Nesa,

I already paste the script as validation script of "Text1" field, but fields from Text2 to Text9 still empty. What can I do to populate each word in the respective fields?

Thank you.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022

In your first post you writed 'text' not 'Text' so if your fields are named 'Text' change that in script. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022
LATEST

You should also look in the console window to see if any errors are reported.  Press "Ctrl-J".

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines