how to remove first special character from string - javascript

I have values seperated by pipes in a database. But the issue is that I am appending | at every entry.
For Example:
|275634|374645|24354|
How can I remove the first pipe from the whole string not all the pipes.
Once inserted I don't need to check for the next time when it updates.
If I use substring(1) then it will remove the first character every time,
Please suggest a fix?

//input = '|275634|374645|24354|';
output = input.replace('|', '');
String#replace will replace the first occurance in a String. If you replace it with an empty String '' it is removed.
jsFiddle

you can use substring(index) method where ever you want to remove the perticular special character from the String: like
String str2 = "|275634|374645|24354|";
str2 = str2.substring(1);
System.out.println(str2);
you can see the output as 275634|374645|24354|

Related

Split and grab text before second hyphen

I have the following text string:
test-shirt-print
I want to filter the text string so that it only returns me:
test-shirt
Meaning that everything that comes after the second hyphen should be removed including the hyphen.
I am thinking that the solution could be to split on hyphen and somehow select the two first values, and combine them again.
I am unaware of which functionality is best practice to use here, I also thinking that if it would be possible to use a regular expression in order to be able to select everything before the second hyphen.
You can use split slice and join together to remove everything after the second hyphen
var str = "test-shirt-print";
console.log(str.split("-").slice(0, 2).join('-'))
You can try with String.prototype.slice()
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
and String.prototype.lastIndexOf()
The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.
var str = 'test-shirt-print';
var res = str.slice(0, str.lastIndexOf('-'));
console.log(res);
You can also use split() to take the first two items and join them:
var str = 'test-shirt-print';
var res = str.split('-').slice(0,2).join('-');
console.log(res);

How to properly replace() on a string

I am trying to use replace with a while loop. I want to replace the first letter in the string with an empty string if the letters is not a vowel. The regex I have used is working because the letters are added to the end of the string, just not sure what is happening with the replace function?
Here is my code:
vowel = new RegExp("[aeiou]");
word = "cherry";
var moved = '',
i = 0;
while (!vowel.test(word[i])) {
moved += word[i];
word.replace(word[i], '');
i++;
}
return word+moved;
For example, 'cherrych' will be returned rather than 'errych'
You don't need a loop here, just use standard regex multiple selectors, e.g. see the following:
'cherrych'.replace(/^[^aeiou]*/, '')
String.prototype.replace returns a modified copy of the string, and does not alter the string in place.
Every time you do word.replace(), you cause a new string to be returned but importantly word is not altered at all.
The right way to attack this is then to assign this new modified copy to the original by
word = word.replace(word[i],'');

How to replace or strip the [number] from string with jquery

The value of selected variable is "Topic [9]" or "Category [10]". i want to remove "[number]" from the value of selected variable?
selected = $('#topic_id option:selected').text();
i think, i can strip value with replace() as follows but need to know, how do i achieve because number is not fixed.
selected = $('#topic_id option:selected').text().replace();
This has nothing to do with jQuery but with pure Javascript. Just use regex, like this one \[.+ to do it.
selected = $('#topic_id option:selected').text().replace(/\[.+/g, "");
Demo.
Of course this only works on the pattern you have provided: With [number] at the end of the string.
UPDATE: As #Zack pointed on the comments, you can use this regex \s\[.+ to remove the space before the [.
Ohjay44 was onto something with the split, but didn't provide an answer, so I went ahead.
This solution will split the selected string by the space character, then call pop() which removes the last element of the array, then it uses join(" ") which returns the elements of an array as a string, concatenated with the provided seperator, or , if none is provided.
Here is a working example on jsfiddle.net http://jsfiddle.net/jX36T/
var selected = "Topic of conversation [9]";
var splitted = selected.split(" ");
splitted.pop();
var string = splitted.join(" ");
alert(stringResult);
Displays "Topic of conversation"
If you only have one word to get, you can explode with the space character and keep the first item of the returned array

How to read only part of a string in Javascript

I am pulling in a string from another web page. I want to read that string into a variable but only after a certain point. Eg:
#stringexample
var variable;
I want variable to equal stringexample but not contain the # how could I do this?
This is how I am using the variable at the moment.
$("#Outputajax").load("folder/"+ variable +".html");
This is the way that works but isn't a variable.
$("#Outputajax").load("folder/webpage.html");
If you just want to trim of the first character, then you can use substring...
var input = "#stringexample";
input = input.substring(1);
//input = "stringexample"
Here is a working example
var myVariable = stringExample.replace('#','');
Could just use variable.substr(1) to cut off the first character.
If you want to specifically remove the hash from the start (but do nothing if the hash isn't there), try variable.replace(/^#/,"")
I understand you want to get everything in the string AFTER the hashtag. The other solutions will leave anything ahead of the hashtag in as well. And substring does not work if the hashtag is not the first symbol.
variable= "#stringexample".split("#")[1];
This splits the string into an array of strings, with the parameter as the point where to split, without including the parameter itself. There will be an empty string as the first parameter, and everything after the hashtag is the second string.
var slicer = function(somestring){
var parsedString = somestring;
parsedString = parsedString.slice(1);
return parsedString
}
// run from yors function with some string
var someYouVar = slicer("#something")

replacing spaces in a string with hyphens

I have a string and I need to fix it in order to append it to a query.
Say I have the string "A Basket For Every Occasion" and I want it to be "A-Basket-For-Every-Occasion"
I need to find a space and replace it with a hyphen. Then, I need to check if there is another space in the string. If not, return the fixed string. If so, run the same process again.
Sounds like a recursive function to me but I am not sure how to set it up. Any help would be greatly appreciated.
You can use a regex replacement like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s/g, "-");
The "g" flag in the regex will cause all spaces to get replaced.
You may want to collapse multiple spaces to a single hyphen so you don't end up with multiple dashes in a row. That would look like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s+/g, "-");
Use replace and find for whitespaces \s globally (flag g)
var a = "asd asd sad".replace(/\s/g,"-");
a becomes
"asd-asd-sad"
Try
value = value.split(' ').join('-');
I used this to get rid of my spaces. Instead of the hyphen I made it empty and works great. Also it is all JS. .split(limiter) will delete the limiter and puts the string pieces in an array (with no limiter elements) then you can join the array with the hyphens.

Categories