This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I would like to replace multiple words from below string:
\njava developer\n
How do I replace \n from start and \n from last from above string?
I used
replace('\n', '')
but it replace first \n only.
If you want to replace all \n without calling replace in a loop you have to use a regular expression. You can use it like this:
var test = "\njava developer\n";
var result = test.replace(/\n/g, '');
the g in the regular expression means replace all occurrences.
Hope this helps.
Thanks vincent!
It works for me.
I implement like this
stringToChange.toLowerCase().toString().replace(/[<b></b>\n]/g,'')
to replace
<\b>, <b> and \n
all multiple occurrences.
Related
This question already has answers here:
Split string into array of characters?
(7 answers)
Closed 3 years ago.
can you help me on this?
I have a simple string:
str="Hello World";
I want to split it as that :
array= str.Split("",System.StringSplitOptions.RemoveEmptyEntries);
result shoud be
array[0]="H"
array[1]="e"
array[2]="l"
array[3]="l"
array[4]="o"
array[5]="W"
array[6]="o"
...
But I don't know to "wildcard" the separator..
Any Idea on this ?
Thanks
?
Just use String.ToCharArray():
SomeArray = str.ToCharArray()
This question already has answers here:
How do you print a dollar sign $ in Dart
(2 answers)
Closed 4 years ago.
I'm currently creating an simple application with flutter(Dart). And when i want to create the AppBar widget, i cant give it names with '$'. Because, same like Kotlin..Dart is using '$' for summoning an identifier.
Any solution?
var _appbar = AppBar(
title: Text("How I Made $100"), //$ is the error
);
The dollar sign is a special character, so if you want it to be ignored you have to escape it with a \.
For example:
void main() {
print("This string contains a dollar \$ign");
}
See this gist.
This question already has an answer here:
Appending a string variable to a fixed string in Perl
(1 answer)
Closed 5 years ago.
I am trying to read input of two strings from the user keyboard, store them in two variables and concatenate the two strings together using the Perls dot operator.
Research I found online shows an example similar to what I am trying to accomplish. This example uses only one string variable in the concatenation but I think something similar should be able to concatenate multiple variables together:
$name = checkbook';
$filename = '/tmp/' . $name . '.tmp';
#$filename now contains "/tmp/checkbook.tmp"
(http://alvinalexander.com/perl/edu/articles/pl010003.shtml)
my code is displayed in the following - however, I am still getting the undesired concatenation :
$stringa=<STDIN>;
$stringb=<STDIN>;
print $stringa.$stringb;
compiled using perl (path)
output
nein
ja
nein
ja
instead of the desired output:
nein
ja
neinja
why am I not getting the concatenation output I think it should produce?
You can use "chomp" to remove the trailing string "\n", like this:
$stringa=<STDIN>;
$stringb=<STDIN>;
chomp($stringa);
chomp($stringb);
print $stringa.$stringb;
I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I find the words consist of the letters inside the keyword in Lua?
I am trying to find words consist of letters inside the keyword. However, the letters shouldn't be used again once it's detected that it's inside the word.
i keep the words in the dictionary.
For example I have the keyword: "asdasdas"
My code says hi in these conditions
consistLetters("asdasdas","asdas")
consistLetters("asdasdas","sad")
consistLetters("asdasdas","sss")
However it also says hi when i consume all of the "s" in the keyword which is not what i want:
consistLetters("asdasdas","ssss")
How to stop this? Thank you.
EDIT: I solved my problem and shared it as an answer. I hope it'll be helpful.
Here is my code:
function consistLetters(keyword,word)
keywordletters={ }
wordletters= { }
local found=false
findLetters(keyword,keywordletters)
findLetters(word,wordletters)
for i=1, #wordletters,1 do
for j=1, #keywordletters,1 do
if(keywordletters[j]~="") then
if(wordletters[i]==keywordletters[j]) then
keywordletters[j]=""
found=true;
break
end
end
end
if found~=true then
return false
end
found=false;
end
end