5

I have string like this:-

var src=url("http://localhost:200/assets/images/eyecatcher/6/black6.png)"

And now I want to get image name i.e black6.png and folder name 6. I know there is substr function I can use but the file name and folder name will be dynamic like orange12.png and 12 etc.

How I can get these values? Please help me.

Thanks

5
  • Did you mean var src="url('http://localhost:200/assets/images/eyecatcher/6/black6.png')"? What have you tried? Commented Aug 18, 2014 at 12:39
  • do you have any pattern for filename / foldername? what is constant about those two? Commented Aug 18, 2014 at 12:40
  • Do you mean the string value include the var src= part? Your question is not clear. Commented Aug 18, 2014 at 12:40
  • I know you have asked about how to learn C#, but I'm unable to answer your question in time because it had been deleted. I will answer you here. Read my another post at: codeproject.com/Answers/813176/… Commented Oct 8, 2014 at 6:32
  • @mjb thanks for reply yes I delete as that was closed by people with many downvotes Commented Oct 8, 2014 at 6:44

7 Answers 7

7

You can use split method for this:

var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var parsed = src.split( '/' );
console.log( parsed[ parsed.length - 1 ] ); // black6.png
console.log( parsed[ parsed.length - 2 ] ); // 6
console.log( parsed[ parsed.length - 3 ] ); // eyecatcher

etc.

Sign up to request clarification or add additional context in comments.

1 Comment

FYI it should be parsed.length -1 to get the image name, and so on.
4

If the base URL is always the same you could do

var url = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var bits = url.replace("http://localhost:200/assets/images/eyecatcher/", "").split("/");
var folder = bits[0], // 6
    file = bits[1];  // black6.png

Comments

3

If your string is:

var src="http://localhost:200/assets/images/eyecatcher/6/black6.png"

Use the following:

var parts = src.split('/');
var img = parts.pop(); //black6.png
var flder = parts.pop(); //6
var sflder = parts.pop(); //eveatcher

Comments

2

You may try like this:

var str = myString.split('/');
var answer = str[str.length - 1];
var answer1 = str[str.length - 2];
var answer2 = str[str.length - 3];

Comments

2
var img_name = src.split('/')[7];
var folder_name = src.split('/')[6];

Comments

2

Using split & slice, Say like bellow

var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var arr = src.split('/').slice(-2) //returns ["6", "black6.png"]
arr[0] //folderName
arr[1] //filename

Comments

2
var str = "http://MachineName:200/assets/images/eyecatcher/6/black6.png";

var newStr = str.split("/");  

ubound = newStr.length;

fileName = newStr[ubound-1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.