How to concatenate without spaces? - string

Dart provide us a new way to concate strings without the + operator.
Old way would be:
String foo = "foo";
String newString = "Hello" + " foo " + "bar";
The dart way would be:
String foo = "foo";
String newString = "Hello $foo bar";
Both would result in:
Hello foo bar
But, what if I want to concatenate without spaces?
Old way would be:
String foo = "foo";
String newString = "Hello" + "foo" + "bar";
Result would be:
Hellofoobar
But when I try this at Dart, it gives me an obviously syntax error:
String foo = "foo";
String newString = "Hello $myString bar";
What is the solution to this? Should I use the String.concat? A string buffer? I really liked this new way to concatenate Strings, but I don't think I could use to this kind of situation.
Thanks in advance.

Multiple options exist.
First instead of using the + you can just have multiple string literals:
String str = 'foo' ' bar ' 'zap'; // any whitespace between literals
Secondly if you want to use string interpolation, you can just as the parens to help with scope:
String foo = 'foo';
String str = 'Hello${foo}bar';

Related

How to replace a character in a string?

The question is, that if i have a string, i.e:
"--Julius.",
and I want replace "-" and ".". How i can get it?
Finally, I get:
"Julius"
This question have an easy solution. You only do the next:
word="--Julius."
word.replace("--", "")
word.replace(".", "")
print(word)
Output:
"Julius"
This is specific for the JavaScript.:
var str ='--Julius.';
str = str.replace(/[-.]/g, function(m) { return {'-':'','.':''}[m]; });
console.log(s);
We can use the .replace(oldvalue, newvalue) function and replace multiple characters in a string in the similar manner:
var str ='--Julius.';
str = str.replace(/[-.]/g, '');
console.log(str);
We could use this instead to replace all the character with the same character.

how to extract before and after substring in a string using python3

I am having a string like "123/abc/${TEST}/ABCD/". I need to extract two strings before and after a "/${" and "}" in two variables.
Expected output is
string1 = 123/abc
string2 = /ABCD/
Using re.split on the pattern /\$\{.*?\}:
inp = "123/abc/${TEST}/ABCD/"
parts = re.split(r'/\$\{.*?\}', inp)
print(parts)
This prints:
['123/abc', '/ABCD/']
We could make this more specific by spitting on /${TEST} exactly, or we could even let TEST come from a string variable.

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

Remove a character from a string without knowing its position

I want to remove a character ('.') from a string without knowing its position.
For example.
string test = "4.000";
But the '.' will always change.
Thanks!
If you only want to remove the first occurrence of that character:
string newString = test.Remove(test.IndexOf(".", 1));
If you want to remove all occurrences of that character:
string newString = test.Replace(".", "");
Use String.Replace.
string stringToReplace = ".";
string test = "4.000";
test = test.Replace(stringToReplace, "");

How can I join a list of strings in specman?

I have a list that I want to print:
foo: list of string;
I want to create a string bar that is the concatenation of the elements of foo. In Perl I would do:
$bar = join " ", #foo;
The only way I can think of to do this in specman is:
var bar: string = "";
for each in foo {
bar = appendf("%s %s", bar, it);
};
This seems like it would have very poor performance, because it copies bar onto itself for each element in foo. Is there any better way to do this?
There is also a dedicated function for this:
str_join(list: list of string, separator: string) : string
I'm sure help str_join will give you the details. There are also other useful functions like str_match, str_split which you may like.
As an additional hint, maybe you should print yourself the e Language Quick Reference, see http://www.cadence.com/Community/blogs/fv/archive/2009/06/19/send-us-suggestions-for-updating-the-e-specman-quick-reference-card.aspx.
While writing the question I stumbled across the to_string() method. I can use:
var bar: string = foo.to_string();
This is the equivalent of Perl's:
$bar = join "\n", #foo;
If I want to use spaces I can use:
var bar: string = str_replace(foo.to_string(), "\n", " ");

Resources