split string

Community Beginner ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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

TOPICS
How to, JavaScript

Views

263

Likes

translate

Translate

Translate

Report

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

correct answers 3 Correct Answers

Adobe Community Professional , 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.

Likes

translate

Translate

Translate
Adobe Community Professional , 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 elementand shift(), removes first element of array and returns that element.

Likes

translate

Translate

Translate
Adobe Community Professional , 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;

Likes

translate

Translate

Translate
Adobe Community Professional ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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 ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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?

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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 ,
Apr 12, 2021 Apr 12, 2021

Copy link to clipboard

Copied

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?

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 12, 2021 Apr 12, 2021

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 11, 2021 Apr 11, 2021

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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 ,
Apr 12, 2021 Apr 12, 2021

Copy link to clipboard

Copied

Thanks Thom Il take a look at it.

Likes

translate

Translate

Translate

Report

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 ,
Apr 21, 2021 Apr 21, 2021

Copy link to clipboard

Copied

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".

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Apr 21, 2021 Apr 21, 2021

Copy link to clipboard

Copied

Use this:

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

Likes

translate

Translate

Translate

Report

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