Parse one string to another [duplicate] - string

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 9 years ago.
string A = "myString";
string B;
Is there a way to initiate B according to the data of A, so that value B changes with A.
B = capture change of A?
Edit: My initial post was not complete and misleading, I found the answer now. Still my question is a duplicate of observer pattern

Try this
B = A.Substring(0,2);
It initates B with substring of A from index 0 and lenght of 2

Sure, using String.Substring:
B = A.Substring(0,2); //"my"

You can try the following example
string A = "myString";
string B;
if (A.StartsWith("my"))
{
B = A.Substring(0, 2);//first two elements of A
}
Check out this Substring method

Related

Groovy : how to fetch last 3 characters of a random length text? [duplicate]

This question already has answers here:
How to get last 2 digit from a String
(7 answers)
Closed 1 year ago.
input :
DEPT MGR_AEG
output :
AEG
above is the input and expected output from piece of Groovy code. I mean I need to fetch the last 3 characters of input string. how to achieve this in Groovy ?
thanks in advance for your help.
reg, Avinash
The simplest way is a substring expression:'
assert 'DEPT MGR_AEG'[-3..-1] == 'AEG'
You can also use take() but it's somewhat unintuative:
assert 'DEPT MGR_AEG'.reverse().take(3).reverse() == 'AEG'

Convert a String representation of number to Integer in Java 8+(single line without if) [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 4 years ago.
This sounds simple, but I can't find a way to convert a String value(possibly null) to Integer in single line without using if else in Java 8+. The answer might involve usage of ofNullable and isPresent.
Some things I have tried:
String x = ...;
Integer.valueOf(x); // fails if x is null
Optional.ofNullable(Integer.valueOf(x)).orElse(null); // NullPointerException
int value = Optional.ofNullable(x).map(Integer::parseInt).orElse(0);
This will result in a default value of 0 if the input String is null.
As an alternative, use:
Integer value = Optional.ofNullable(x).map(Integer::valueOf).orElse(null);
which will result in null if the input String is null.
What about using ? operator instead of one line if...else?
Integer value = x != null ? Integer.valueOf(x) : null;

Concatenate three numbers together in MATLAB [duplicate]

This question already has answers here:
Return vector elements as a single integer
(2 answers)
Closed 7 years ago.
Suppose that we have these three numbers:
a=2;
b=3;
c=5;
I want concatenate these three numbers to have:
out = 235; %// (double variable not string)
How can i do this with and without (prefer this) converting it to string?
A more general approach that Dan's: If you have a vector v of digits, you can convert it into a single number by
v = [a,b,c]; %// [2,3,5]
out = v * (10.^( (numel(v)-1):-1:0 ) )'
In addition to Shai's solution, you can use a combination of num2str and str2num.
v = [a; b; c]; %// [2;3;5]
out = str2num(num2str(v)')
out =
235
Or the faster, but maybe harder to read alternative:
out = str2num(char('0'+v))
Now, if you have to do this many times, you can assign this to an anonymous function handle:
f = #(v) str2num(num2str(v(:))')
Now you can simply do f(v). This will work with both horizontal and vertical arrays, due to the use of (:).
Note that you can turn Shai's approach into a function handle too:
f = #(v) v*(10.^((numel(v)-1):-1:0))';

How to store numbers of a string in a list in python [duplicate]

This question already has answers here:
Convert string (without any separator) to list
(9 answers)
Closed 7 years ago.
I have a string s="12345678"
I need to store these numbers in a list like below format.
s=['1','2','3','4','5','6','7','8']
So can anybody help me how to do this.
Thanks,
Try this:
s = "12345678"
a = [c for c in s]

Convert string to table in lua [duplicate]

This question already has answers here:
String to Table in Lua
(2 answers)
Closed 9 years ago.
I am newbie in lua. I need to convert following string to lua table. How could I do this?
str = "{a=1, b=2, c={d=3,e=4} }"
I want to convert this string to lua table, so that I can access it like this:
print(str['a']) -- Output : 1
print(str['c']['d']) -- Output : 3
You could simply add a str = to the beginning of the string and let the interpreter load that string as a chunk for you. Note that loadstring doesn't run the chunk but returns a function. So you add () to call that function right away and actually execute the code:
loadstring("str = "..str)()
This would do the same thing:
str = loadstring("return "..str)()
If you don't generate the string yourself, that can be dangerous though (because any code would be executed). In that case, you might want to parse the string manually, to make sure that it's actually a table and contains no bad function calls.

Resources