I have a string, and I want to remove a specific section from it.
The situation is as follows:
var input:String = "The quick brown fox jumped over the lazy dog.";
var scrubStart:Number = input.indexOf("br");
var scrubEnd: Number = input.indexOf("la");
var output:String = input.substring(0,scrubStart) + input.substring(scrubEnd,input.length);
I have it set up like this because the input text is different every time, and the section I want to remove is in a different place every time, but the content of the section is the same.
The code I have doesn't work, and it returns everything after scrubStart. Where have I gone wrong?
Code from your question appears to operate as expected, resulting in:
The quick lazy dog.
This could be accomplished using string methods of either slice or substring.
Slice:
const input:String = "The quick brown fox jumped over the lazy dog.";
var output:String = input.slice(0, input.indexOf("br")) +
input.slice(input.indexOf("la"));
Substring:
const input:String = "The quick brown fox jumped over the lazy dog.";
var output:String = input.substring(0, input.indexOf("br")) +
input.substring(input.indexOf("la"));
Regex:
You could also implement regular expressions; although, by pattern instead of index.
const input:String = "The quick brown fox jumped over the lazy dog.";
var pattern:RegExp = /brown.*?the /;
var output:String = input.replace(pattern, "");
Related
If I have a search box and want to find if a string contains certain words (not case sensitive) and/or numbers.
search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"
if string.match(textline, search) then
result = textline
end
just like a web search.
search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"
for item in string.gmatch(search, "%S+") do
if string.find(textline, string.lower(item)) then
result = textline
end
end
You break up the word values you are looking for and convert them into an array. Then you should loop that array and check if your main variable is in it.
If I understood correctly what you want to do this should solve your problem.
In Python, I can splice string like this:
solo = A quick brown fox jump over the lazy dog
solo[3:5]
I know substr and comb is enough, I want to know if it's possible, However. Should I use a role to do this?
How to slice a string
Strings (represented as class Str) are seen as single values and not positional data structures in Perl 6, and thus can't be indexed/sliced with the built-in [ ] array indexing operator.
As you already suggested, comb or substr is the way to go:
my $solo = "A quick brown fox jumps over the lazy dog";
dd $solo.comb[3..4].join; # "ui"
dd $solo.comb[3..^5].join; # "ui"
dd $solo.substr(3, 2); # "ui"
dd $solo.substr(3..4); # "ui"
dd $solo.substr(3..^5); # "ui"
If you want to modify the slice, use substr-rw:
$solo.substr-rw(2..6) = "slow";
dd $solo; # "A slow brown fox jumps over the lazy dog"
How to make operator [ ] work directly on strings
If you really wanted to, you could compose a role into your string that adds method AT-POS, which would make the [ ] operator work on it:
my $solo = "A quick brown fox jumps over the lazy dog" but role {
method AT-POS ($i) { self.substr($i, 1) }
}
dd $solo[3..5]; ("u", "i", "c")
However, this returns slices as a list, because that's what the [ ] operator does by default. To make it return a concatenated string, you'd have to re-implement [ ] for type Str:
multi postcircumfix:<[ ]>(Str $string, Int:D $index) {
$string.substr($index, 1)
}
multi postcircumfix:<[ ]>(Str $string, Range:D $slice) {
$string.substr($slice)
}
multi postcircumfix:<[ ]>(Str $string, Iterable:D \slice) {
slice.map({ $string.substr($_, 1) }).join
}
my $solo = "A quick brown fox jumps over the lazy dog";
dd $solo[3]; # "u"
dd $solo[3..4]; # "ui"
dd $solo[3, 5]; # "uc"
You could extend the [ ] multi-candidates added here to handle all the other magic that the operator provides for lists, like:
from-the-end indices, e.g. $solo[*-1]
truncating slices, e.g. $solo[3..*]
adverbs, e.g. $solo[3..5]:kv
assignment, e.g. $solo[2..6] = "slow"
But it would take a lot of effort to get all of that right.
Also, keep in mind that overriding built-in operators to do things they weren't supposed to do, will confuse other Perl 6 programmers who will have to review or work with your code in the future.
I need to know if two strings "match" where "matching" basically means that there is significant overlap between the two strings. For example, if string1 is "foo" and string2 is "foobar", this should be a match. If string2 was "barfoo", that should also be a match with string1. However, if string2 was "fobar", this should not be a match. I'm struggling to find a clever way to do this. Do I need to split the strings into lists of characters first or is there a way to do this kind of comparison already in Groovy? Thanks!
Using Apache commons StringUtils:
#Grab( 'org.apache.commons:commons-lang3:3.1' )
import static org.apache.commons.lang3.StringUtils.getLevenshteinDistance
int a = getLevenshteinDistance( 'The quick fox jumped', 'The fox jumped' )
int b = getLevenshteinDistance( 'The fox jumped', 'The fox' )
// Assert a is more similar than b
assert a < b
Levenshtein Distance tells you the number of characters that have to change for one string to become another
So to get from 'The quick fox jumped' to 'The fox jumped', you need to delete 6 chars (so it has a score of 6)
And to get from 'The fox jumped' to 'The fox', you need to delete 7 chars.
As per your examples, plain old String.contains may suffice:
assert 'foobar'.contains('foo')
assert 'barfoo'.contains('foo')
assert !'fobar'.contains('foo')
here is my code
var sig = crypto.createHash('md5')
.update('The quick brown fox jumps over the lazy dog')
.digest('base64');
console.log(sig)
results in nhB9nTcrtoJr2B01QqQZ1g== (on Mac OS X).
I'm trying to generate the same signature from an ios app. The results are the same in objective c as in online converter sites: the string
The quick brown fox jumps over the lazy dog
converted to md5, I get
9e107d9d372bb6826bd81d3542a419d6,
and the base64 of this is OWUxMDdkOWQzNzJiYjY4MjZiZDgxZDM1NDJhNDE5ZDY=.
Why are those strings different? Isn't this what nodejs crypto module is doing?
What are the equivalent of nodejs algorithm for getting the md5 hash digested with base64?
The string OWUxMDdkOWQzNzJiYjY4MjZiZDgxZDM1NDJhNDE5ZDY= is the base64 encoded version of the string 9e107d9d372bb6826bd81d3542a419d6 which is in it self the md5 hash of the plain text string The quick brown fox jumps over the lazy dog.
If you want to do this in node you first have to get the md5 hash in hex:
var crypto = require('crypto');
var s = 'The quick brown fox jumps over the lazy dog';
var md5 = crypto.createHash('md5').update(s).digest('hex');
Now you have the md5 hash as hex (9e107d9d372bb6826bd81d3542a419d6). Now all you have to do is convert it to base64:
new Buffer(md5).toString('base64');
I have this string:
"The quick brown f0x jumps 0ver the lazy d0g, the quick brown f0x jumps 0ver the lazy d0g.".
I need a function that will replace all zeros between "brown" and "lazy" with "o". So the output will look like this:
"The quick brown fox jumps over the lazy d0g, the quick brown fox jumps over the lazy d0g.".
So it will look all over the string and most importantly will leave all other zeros intact.
function(text, leftBorder, rightBorder, searchString, replaceString) : string;
Is there any good algorithm?
If you have Python, here's an example using just string manipulation, eg split(), indexing etc. Your programming language should have these features as well.
>>> s="The quick brown f0x jumps 0ver the lazy d0g, the quick brown f0x jumps 0ver the lazy d0g."
>>> words = s.split("lazy")
>>> for n,word in enumerate(words):
... if "brown" in word:
... w = word.split("brown")
... w[-1]=w[-1].replace("0","o")
... word = 'brown'.join(w)
... words[n]=word
...
>>> 'lazy'.join(words)
'The quick brown fox jumps over the lazy d0g, the quick brown fox jumps over the lazy d0g.'
>>>
The steps:
Split the words on "lazy" to an array A
Go through each element in A to look for "brown"
if found , split on "brown" into array B. The part you are going to change is the
last element
replace it with whatever methods your programming language provides
join back the array B using "brown"
update this element in the first array A
lastly, join the whole string back using "lazy"