12

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
  • 423
  • 1
  • 5
  • 15

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,423
  • 1
  • 13
  • 26
6

var str = "TEST NAME\\TEST ADDRESS"; // Show: TEST NAME\TEST ADDRESS
console.log(str);
var res = str.split("\\");
console.log(res);
user2226755
  • 12,494
  • 5
  • 50
  • 73
5
var mystring = 'TEST NAME/TEST ADDRESS';
var splittable = mystring.split('/');
string1 = splittable[0];
string2 = splittable[1];
Beastoukette
  • 99
  • 1
  • 2
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,935
  • 6
  • 24
  • 55