Scraping strings for numbers - node.js

So I have a bunch of strings that look like this:
var string = "0.1 - 1";
I'd like to pull out the value that's to the left of "-" as well as the value to the right. In this case that would be:
leftValue = 0.1, rightValue = 1. Any idea how to achieve this?

This will work assuming your string is always a number, a dash, and another number. It will not work with negative numbers since it splits on all dash characters.
var string = "0.1 - 1";
var splitStr = string.split("-");
var leftValue = parseFloat(splitStr[0]);
var rightValue = parseFloat(splitStr[1]);

Related

How can I change only vowels from uppercase to lowercase and vice versa (MATLAB)

I have to change every vowel on a string to upper or lower case depending what it already is.. so "UPPERCASE lowercase" becomes "uPPeRCaSe lOwErcAsE"
So far I have had no success with this aproach
str= 'UPPERCASE lowercase';
vow = 'aeiou';
vowm = 'AEIOU';
for k = 1:5
if str(str == vow(k))
str(str == vow(k))= vowm(k);
else
if str(str == vowm(k))
str(str == vowm(k))= vow(k);
Expected output: "uPPeRCaSe lOwErcAsE"
Actual output: "uPPERCASE lOwErCAsE"
I am extremely new to matlab and im kinda lost.
i aprecciate your help
Use ismember to find all occurrences of each type of vowels (uppercase or lowercase), and then upper and lower to convert them:
str = 'UPPERCASE lowercase'; %// original string
indl = ismember(str, 'aeiou'); %// locations of lowercase vowels
indu = ismember(str, 'AEIOU'); %// locations of uppercase vowels
str(indl) = upper(str(indl)); %// convert from lower to upper
str(indu) = lower(str(indu)); %// convert from upper to lower
As listed in the question, I am assuming the following as the inputs -
%// Inputs
str= 'UPPERCASE lowercase'
vow = 'aeiou'
vowm = 'AEIOU'
Approach #1
One approach based on changem that is used to substitute values -
%// Create maps from input string to reflect changes from lower to upper
%// and vice versa
map1 = changem(str,vowm,vow)
map2 = changem(str,vow,vowm)
%// Find indices to be changed for lower to upper change and vice versa change
idx1 = find(map1~=str)
idx2 = find(map2~=str)
%// Selectively change input string based on the indices to be changed and maps
str(idx1) = map1(idx1)
str(idx2) = map2(idx2)
Approach #2
With bsxfun -
%// Find indices to be changed for lower to upper change and vice versa change
[~,idx1] = find(bsxfun(#eq,str,vow'))
[~,idx2] = find(bsxfun(#eq,str,vowm'))
%// Selectively change input string based on the indices to be changed and maps
str(idx1) = str(idx1)-32
str(idx2) = str(idx2)+32
You could use regular expressions
as well.
I don't know how different this is to the other answers though...
str= 'UPPERCASE lowercase';
vow = '[aeiou]';
vowm = '[AEIOU]';
indl = regexp(str,vow);
indu = regexp(str,vowm);
str(indl) = upper(str(indl));
str(indu) = lower(str(indu));

Swift how to convert hash value number of text back into text

I am using swift to get some text a user puts into the textfield, and then I want to get the hash-value number of the characters they put in. I have this code to get the text and each of its elements
var plaintext = textField.text
var countedtext = countElements(plaintext)
for var index = 0; index < countedtext; ++index {
let idx = advance(plaintext.startIndex, index)
var element = plaintext[idx]
var n = 1
var newnumber = element.hashValue + n;
newtext.append(newcharter)
I use var newnumber = element.hashValue + n; To get the hash value of the character and plus it by one to get the letter after that letter. So if I was to type in "A" it would get the hash value number plus it by one and convert that number back into text form which would be "B". I check to see would this work by getting both hash values of A and B, "A" was 4799450059485595655 and "B" was 4799450059485595656. I searched google but was not able to find a way to do this.
Thanks in advance
Hash functions are unidirectional, that is they are not be reversible—by design.
What are you trying to accomplish?

sort string according to first characters matlab

I have an cell array composed by several strings
names = {'2name_19surn', '3name_2surn', '1name_2surn', '10name_1surn'}
and I would like to sort them according to the prefixnumber.
I tried
[~,index] = sortrows(names.');
sorted_names = names(index);
but I get
sorted_names = {'10name_1surn', '1name_2surn', '2name_19surn', '3name_2surn'}
instead of the desired
sorted_names = {'1name_2surn', '2name_19surn', '3name_2surn','10name_1surn'}
any suggestion?
Simple approach using regular expressions:
r = regexp(names,'^\d+','match'); %// get prefixes
[~, ind] = sort(cellfun(#(c) str2num(c{1}), r)); %// convert to numbers and sort
sorted_names = names(ind); %// use index to build result
As long as speed is not a concern you can loop through all strings and save the first digets in an array. Subsequently sort the array as usual...
names = {'2name_2', '3name', '1name', '10name'}
number_in_string = zeros(1,length(names));
% Read numbers from the strings
for ii = 1:length(names)
number_in_string(ii) = sscanf(names{ii}, '%i');
end
% Sort names using number_in_string
[sorted, idx] = sort(number_in_string)
sorted_names = names(idx)
Take the file sort_nat from here
Then
names = {'2name', '3name', '1name', '10name'}
sort_nat(names)
returns
sorted_names = {'1name', '2name', '3name','10name'}
You can deal with arbitrary patterns using a regular expression:
names = {'2name', '3name', '1name', '10name'}
match = regexpi(names,'(?<number>\d+)\D+','names'); % created with regex editor on rubular.com
match = cell2mat(match); % cell array to struct array
clear numbersStr
[numbersStr{1:length(match)}] = match.number; % cell array with number strings
numbers = str2double(numbersStr); % vector of numbers
[B,I] = sort(numbers); % sorted vector of numbers (B) and the indices (I)
clear namesSorted
[namesSorted{1:length(names)}] = names{I} % cell array with sorted name strings

Is it possible to concatenate a string with series of number?

I have a string (eg. 'STA') and I want to make a cell array that will be a concatenation of my sting with a numbers from 1 to X.
I want the code to do something like the fore loop here below:
for i = 1:Num
a = [{a} {strcat('STA',num2str(i))}]
end
I want the end results to be in the form of {<1xNum cell>}
a = 'STA1' 'STA2' 'STA3' ...
(I want to set this to a uitable in the ColumnFormat array)
ColumnFormat = {{a},... % 1
'numeric',... % 2
'numeric'}; % 3
I'm not sure about starting with STA1, but this should get you a list that starts with STA (from which I guess you could remove the first entry).
N = 5;
[X{1:N+1}] = deal('STA');
a = genvarname(X);
a = a(2:end);
You can do it with combination of NUM2STR (converts numbers to strings), CELLSTR (converts strings to cell array), STRTRIM (removes extra spaces)and STRCAT (combines with another string) functions.
You need (:) to make sure the numeric vector is column.
x = 1:Num;
a = strcat( 'STA', strtrim( cellstr( num2str(x(:)) ) ) );
As an alternative for matrix with more dimensions I have this helper function:
function c = num2cellstr(xx, varargin)
%Converts matrix of numeric data to cell array of strings
c = cellfun(#(x) num2str(x,varargin{:}), num2cell(xx), 'UniformOutput', false);
Try this:
N = 10;
a = cell(1,N);
for i = 1:N
a(i) = {['STA',num2str(i)]};
end

splitting a string in matlab

In order to make this question easier to describe I have provided the following example code, which is similar to the actual data I am working with:
clear all
AirT = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)};
SolRad = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)};
Rain = {rand(32,1),rand(32,1),rand(32,1),rand(32,1)};
Location = {'England','Wales','Scotland','Ireland'};
points = {'old','old','old','new'};
CorrVariables = {'AirT','SolRad','Rain'};
for i = 1:length(Location);
Data = #(location) struct('Location',location,CorrVariables{1},AirT{i},...
CorrVariables{2},SolRad{i},CorrVariables{3},Rain{i});
D(i) = Data(Location{i});
end
FieldName = {D.Location};
R = corrcoef([D.AirT],'rows','pairwise');
R_Value = [Location(nchoosek(1:size(R,1),2)) num2cell(nonzeros(tril(R,-1)))];
q = points(nchoosek(1:size(R,1),2));
%to calculate the combination of these points we need to convert the
%cell into a matrix.
Re = [R_Value q];
From this example I would like to create another cell array in column 5 of Re which is dependant on the strings in columns 4 and 5. So, if columns 4 and 5 in Re are equal, such as 'old''old' then column 6 should show 'old'. However, if the cells differ e.g. 'old' 'new' then I would like the new cell array (i.e. column 6 in Re) to state 'old/new'.
How would this be possible?
From your description I think the clearest approach is to use a combination of string concatenation and regular expressions.
First combine columns 4 and 5 into a new column:
newColumn = strcat(Re(:,4), '/', Re(:,5));
Now look for the repeated pattern and replace with the first token matched:
newColumn = regexprep(newColumn, '(\w+)/\1', '$1');
Combine into existing cell matrix:
Re = [Re, newColumn];

Resources