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(' ');
////////
Related
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]}');
I have names of some persons which are given by the user so when I want to save the list of users as a string so that I can store it in SQLite DB and also share it through dynamic links can it be like the names separated by commas or full stops etc which can be again converted to a list?
You can use 'split' to split a String into List. And then use 'join' to join the List back to string. Please see the code below where I use | as a separator.
String str = "user1|user2|user3|user4|user5";
List<String> strLst = str.split("|");
print(strLst); //[user1, user2, user3, user4, user5]
str = strLst.join("|");
print(str); //user1|user2|user3|user4|user5
I am trying to split a string into individual characters.
The string I want to split: let lastName = "Kocsis" so that is returns something like: ["K","o","c","s","i","s"]
So far I have tried:
var name = lastName.componentsSeparatedByString("")
This returns the original string
name = lastName.characters.split{$0 == ""}.map(String.init)
This gives me an error: Missing argument for parameter #1 in call. So basically it does't accept "" as an argument.
name = Array(lastName)
This does't work in Swift2
name = Array(arrayLiteral: lastName)
This doesn't do anything.
How should I do this? Is There a simple solution?
Yes, there is a simple solution
let lastName = "Kocsis"
let name = Array(lastName.characters)
The creation of a new array is necessary because characters returns String.CharacterView, not [String]
I am trying to find a way to see if a string is included in a field name.
fieldName = 'OneTwoThree';
I want
findTwo == true if fieldName contains char 'Two' somewhere in string
any suggestions?
You can use fieldnames and then strfind.
a.OneTwoThree = 4; %// first field name
a.AnotherField = 'hello'; %// second example field name
To test all field names:
names = fieldnames(a); %// gives all field names
findTwo = ~isempty(strfind(names,'Two'));
To test only first field:
names = fieldnames(a); %// gives all field names
findTwo = ~isempty(strfind(names{1},'Two'));
I have maybe a silly question. Let's say that we have a string:
"my name <em>is</em> Tom <em>Papas</em> and I am 30 <em>years</em> of age<em>!</em>"
The question is: how do we extract the substrings that are enclosed within the <em> tags and output them as a list, array or a comma delimited string using coldfusion? Notice that we don't know what substrings are enclosed within the tags. We need to extract substrings blindly.
Thank you in advance,
Tom
Greece
Download jsoup and put the jar in your CF's lib folder
html = "my name <em>is</em> Tom <em>Papas</em> and I am 30 <em>years</em> of age<em>!</em>";
dom = createObject("java", "org.jsoup.Jsoup").parse(html);
emElements = dom.getElementsByTag("em");
results = [];
for (em in emElements)
arrayAppend(results, em.text());
For more info: http://www.bennadel.com/blog/2358-Parsing-Traversing-And-Mutating-HTML-With-ColdFusion-And-jSoup.htm
Or use basic Regex
matches = rematch("<em>[^<]*</em>", html);
results = [];
for (match in matches)
arrayAppend(results, rereplace(match, "<em>(.*)</em>", "\1") );
In CF 10 or Railo 4, you can combine xmlParse() with Underscore.cfc's map() function, like so:
str = "my name <em>is</em> Tom <em>Papas</em> and I am 30 <em>years</em> of age<em>!</em>";
str = "<myWrapper>" & str & "</myWrapper>";
xmlObj = XmlParse(str);
resultAsArray = _.map(xmlObj.myWrapper.xmlChildren, function (val) {
return val.xmlText;
});
(Disclaimer: I wrote Underscore.cfc)