How to get the first two words in a string Flutter - string

I have this string example: Hello my name is
And this is what I want to show: Hello my
What is the best practice to do it?
Thanks in advance

Split, then take(2) and then join them again:
final src = 'Hello my name is';
final result = src.split(' ').take(2).join(' ');
print(result);

The easiest way is to break down the sentence into a List of words :
String test = 'Hello my name is';
List<String> words = test.split(" ");
print('${words[0]} ${words[1]}');

Related

Split string in multiples lines swift

I'm trying to split a string in multiple lines (such as the text of a song); I'm working in the localizable.strings.
I'd like to do something like this:
"TITLE_SONG01" = "It's my life";
"SUBTITLE_SONG01" = "";
"TEXT_SONG01" = "It's my life" +
"is now or never";
but it doesn't work because data isn't in the correct format. Can anyone help me?
You can do that by inserting "\n" where you want the new line to start. For example:
print("Hello\nWorld")
Which will output the Hello on one line and World on another like this:
Hello
World

How to print specific number of words from a string in scala?

I have a strings:
str = "this is a great place...."
I want to print only 30 words from this string. How to do that?
Use split and take methods:
val str = "this is a great place...."
str.split("\\W").take(30).mkString(" ")
// res0: String = this is a great place
You could just do something like:
"""(\b\w+\b\W*){0,30}""".r findPrefixOf "this is a great place...."
Or using a different notation:
"""(\b\w+\b\W*){0,30}""".r.findPrefixOf("this is a great place....")
Here is some pseudo code you can work with
Split string using the split method into an Array[String] of the words.
Iterate across the array and concatenate the words together that you want to include
Print out the string
I can't think of any external libraries or built-in functions that will do that for you. You will need to write your own code to do this.

Lua: String to name?

is it possible to convert a string into a name of a table entry?
just like
string = "test"
y = {test = 123}
print y.string
So if string is "test" it would print y.test if there is such a entry
Hope you understand my question!
Thanks in advance...
print(y[string])
Be careful with using names such as string for variables, especially globals. You've just overwritten the string table.

Add new character in between a string builder string

I am building a new string using string builder. But now if want to add new characters in between already existing characters in the stringbuilder. How do i do it?
Example code:
StringBuilder sbr = new StringBuilder(" ");
sbr.append(1);
sbr.append(" ");
sbr.append(2);
sbr.append(" ");
sbr.append("3");
sbr.append(" ");
Now the string looks like 1 2 3
I want to add a new string after the number two. Can anyone please guide me how to do that?
Use the following to insert the character at position 3
sbr.insert(2, "<new charactor>");

C# ComboBox Selected Text To Multiple Strings

I have a combobox that has the entries of a full name, eg: John Smith, Mark Tall, etc.
I have written the following:
string FullName = StudentSelectStudentComboBox.Text;
This gets "John Smith" as the string 'FullName'. Is it possible to break down 'FullName' string into 2 strings; FirstName and LastName?
You can just use string.split
string str = "John Adams";
string[] names= str.Split(' '); //names[0]="John" names[1]="Adams"
This answer is similar to the one above but with a twist:
If you want to get fancy you can try:
///////////////
//Names[0] = first name, Name1 = last name
string[] Names = StudentSelectStudentComboBox.Text.Split(' ');
////////

Resources