Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Split A String

I have a variable that holds the value 'website.html'.

How can I split that variable so it only gives me the 'website'?

Thanks

like image 524
Oliver Jones Avatar asked May 05 '12 18:05

Oliver Jones


People also ask

How can I split a string into two JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is the split method in JavaScript?

split() The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

How do you split a string in HTML?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.


2 Answers

var a = "website.html";
var name = a.split(".")[0];

If the file name has a dot in the name, you could try...

var a = "website.old.html";
var nameSplit = a.split(".");
nameSplit.pop();    
var name = nameSplit.join(".");

But if the file name is something like my.old.file.tar.gz, then it will think my.old.file.tar is the file name

like image 196
paulslater19 Avatar answered Sep 27 '22 21:09

paulslater19


String[] splitString = "website.html".split(".");
String prefix = splitString[0];

*Edit, I could've sworn you put Java not javascript

var splitString = "website.html".split(".");
var prefix = splitString[0];
like image 29
K2xL Avatar answered Sep 27 '22 21:09

K2xL