How can I grab just the first letters of a string? [closed] - asp.net

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am passing a username in a session like below and would like to get the first letter of the first name and last name. So for example Doe, John I would like to get "JD" etc. Any idea how I would do this? Thank you
Session("Username")

As an example following your requirements. Lastname stored in first position and firstname stored in last position, but the order of the returned string is First/Last
string username = "Doe, John";
string[] parts = username.Split(' ');
string result = parts[1].Substring(0,1) + parts[0].Substring(0,1);
Console.WriteLine(result);
No error checking is present for clarity, but if you want this for a real work some checks on length of parts is mandatory

If you have only spaces between the parts of the username, try with this RegEx :
initials = Regex.Replace(Session("Username")," *([^ ])[^ ]*","$1")
--> the string "[^ ]" gets every character which is not a space (you can add the char '-' if you want)
By this way, you get a solution which works with people called "John Joe Jibbs" ^^
This is just a proof of concept.

Related

Can a variable inherit elements of other variables?

I'm using the term elements in my question generically to try to make my question easier to understand. I have this simple prompt to enter a name then print it.
prompt = "Enter a name: "
name = input(prompt)
print(f"\nHello, {name} ")
What I'm having trouble understanding is that if the 'prompt' variable is equal to the statement "Enter a name:" why does it not also display the text in the print statement. Is the input function only accepting input from the user and negates the sentence which the variable is connected to. Thanks for helping me to understand this.
Is the input function only accepting input from the user and negates the sentence which the variable is connected to.
Yes, you are right.
input(prompt) only returns the data that the user entered, not the whole line.

Split character in word [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to separate this word, as an example the String is ASP_01, so I want to separate it into two like store ASP in one variable and the 01 in another variable, is it possible for me to do this in asp.net visual basic,
I am newbie in this,
Thanks for helping me.
You need to use the Split Function. Link
dim str As String = 'ASP_01'
dim newWord As String = str.Split(new Char {"_"c})
dim 1stWord As String = newWord[0] //result is "ASP"
dim 2ndWord as String = newWord[1] //result is "01"

Firebase "like" search on string [duplicate]

This question already has answers here:
How to perform sql "LIKE" operation on firebase?
(5 answers)
Closed 5 years ago.
I am trying to search the user on the basis of name . It search perfectly if the name is given from first name but didn't search on the string the second name. Where first and second both are saved in one value separated by space for example
"John Smith". If search on "john" or "jo" etc it retrieve the record but if its smith it retrieve nothing.
Code
var ref = firebase.database().ref().child('User').orderByChild("name").startAt(searchString).endAt(searchString + "\uf8ff");
$scope.usercollection = $firebaseArray(ref);
With your query it is normal that you only get results starting with j, jo, joh, john, etc... The startAt method "creates a Query with the specified starting point". So it looks at how your searchString variable starts.
There is no full text search mechanism in Firebse for the moment. You will find on the web several possibilities to implement such a mechanism, e.g.:
https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html
https://github.com/FirebaseExtended/flashlight

GroupingBy, summing, and return value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Ok, I tried to ask this question earlier and it didn't go well. Maybe this is a better explanation of what I need.
I have a Loan object with over 30 attributes. I want to group by one of the attributes (the loan number, a String), sum another attribute (the loan amount, a Double), and just return the remaining attributes. So I tried:
Map<String, List<Loan>> groupedLoans = loanList.stream()
.collect(groupingBy(Loan::getLoanNum, summingDouble(Loan::getAmount));
However that does not give me a List of Loans. I get only the 2 attributes used in the collect statement, a Map<String, List<Double>>, which isn't what I want (I understand it though). I've lost all the other attributes of the Loan. I want a Map<String, List<Loan>>.
I've gathered from the research I've done that I probably need to 'new' up another Loan object, and pass the attributes in the constructor, but like I said, I have over 30 attributes, and that would be unwieldy.
How can I achieve this elegantly?
Take-2. Let's see if I can tackle it this time. So apparently you need a copy constructor like this first:
public Loan(Loan other, double value) {
this.someProp = other.someProp;
.....
this.value = value;
}
then you still need to collect to a Map with a Comparator that take into consideration the loanNum:
loans.stream().collect(Collectors.groupingBy(Function.identity(),
() -> new TreeMap<>(Comparator.comparing(Loan::getLoanNumber)),
Collectors.summingDouble(Loan::getValue)))
.entrySet()
.stream()
.map(e -> {
return new Loan(e.getKey(), e.getValue());
})
.collect(Collectors.toList());
e.getKey is actually the Loan; while e.getValue is the summing of all grouped values.

Why are pointers used for struct instances in this golang tutorial? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Making a simple api in golang, why does this tutorial use
var movies = map[string]*Movie{
"tt0076759": &Movie{Title: "Star Wars: A New Hope", Rating: "8.7", Year: "1977"},
"tt0082971": &Movie{Title: "Indiana Jones: Raiders of the Lost Ark", Rating: "8.6", Year: "1981"},
}
while this other tutorial uses something more like:
type Movies []Movie
var movies Movies
movies = append(movies, Movie{id: "tt0076759", Title: "Star Wars: A New Hope", Rating: "8.7", Year: "1977"})
It seems like the first one gives me a map containing key value pairs where the value is a pointer to a movie. And the second one gives me an array(slice?) of movies where the id serves as the key for lookup. Why are pointers used in the first one?
First of all you need to understand what pointer is used for. When you want to share the value use Pointer as the Tour of Go
A pointer holds the memory address of a value
As the first tutorial because they wanted to create variable that share value so it uses less memory.
As the second tutorial you don't need to add pointer variable to a slice.
Because Slice it self is a reference type.
Slices hold references to an underlying array, and if you assign one
slice to another, both refer to the same array.
some reference :
https://golang.org/doc/effective_go.html#slices
https://stackoverflow.com/a/2441112/2652524
I glanced through the article and I can't see any particular reason. Perhaps he's implemented something that wasn't covered in that post like a method receiver for *Movie.
Consider this example: https://play.golang.org/p/SZb47MKIr3
It'll fail to compile because the "Print" function has a Pointer Receiver, but the instances in the map are not pointers.
Whereas this example: https://play.golang.org/p/g0aXXcze5d Runs fine.
This is a good post explaining the difference: https://nathanleclaire.com/blog/2014/08/09/dont-get-bitten-by-pointer-vs-non-pointer-method-receivers-in-golang/

Resources