Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Slice a string at character

Im pretty new to js so I hope this isnt a dumb question.

Im trying to slice the username string after a certain character ("."). This is what Ive tried so far.

Original Username: john.smith
The output I want: john

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<html>
<body>

<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>

<p id="demo"></p>

<script>
let text = "John.Smith"; 
let result = text.slice(0, ".");

document.getElementById("demo").innerHTML = result;
</script>

</body>
</html>

There is no output with the ‘.’ parameter.

Does anyone know if there is way to slice the string at the ‘.’ or is there another function. I dont want to use an array in this case.

>Solution :

let result = text.split(".")[0];

I hope this will be helpful for you.
Thanks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading