11

Consider this below string in JavaScript:

"TEST NAME\TEST ADDRESS" 

(it contains only one "\" which cannot be changed).

Now, this above string needs to be split into two string by "\" char.

Resulting strings:

"TEST NAME"
"TEST ADDRESS"

How, can this be done in JavaScript?

Smart B0y
  • 393
  • 1
  • 4
  • 13

4 Answers4

15

Do like this:

var str = "TEST NAME/TEST ADDRESS";
var res = str.split("/");

You will get first part on res[0] and second part on res[1].

Md. Al-Amin
  • 1,401
  • 1
  • 13
  • 25
5
var mystring = 'TEST NAME/TEST ADDRESS';
var splittable = mystring.split('/');
string1 = splittable[0];
string2 = splittable[1];
Beastoukette
  • 91
  • 1
  • 2
5

var str = "TEST NAME\\TEST ADDRESS"; // Show: TEST NAME\TEST ADDRESS
console.log(str);
var res = str.split("\\");
console.log(res);
Sky Voyager
  • 11,413
  • 4
  • 45
  • 70
2

For Backslash or For URL or For Path:

If Suppose, we are getting path like below:

C:\fakepath\yourImage.jpeg

If you want to take filename alone,

var scanImagePath = C:\fakepath\yourImage.jpeg;
choosedFileName = scanImagePath.substring(scanImagePath.lastIndexOf("\\") + 1, scanImagePath.length);
McDonal_11
  • 3,785
  • 6
  • 23
  • 55