How to get the splitted string from split() method - regex

Hi friends I m trying to obtain the splitted first name and last name obtained from the full name entered by user in my jsp form. After receiving the full name on submitting the form , I am splitting the fullname into two strings which is splitted with whitespaces delimiters. Here's the code to explain my problem .Please help
String name = request.getParameter("name");
String userType = request.getParameter("usertype");
//split the name into first_name and last_name
String splittedName[] = StringUtils.split(name);
System.out.println(Arrays.toString(splittedName));
String firstname = splittedName[0];
String surname = splittedName[1];
on debugging the application with breakpoints at the all the lines . I get error when i try to get splittedName[0] and splittedName[1] values into 'firstname' and 'surname' string. Please help ...sorry for any silly mistakes in my question.

String name = "Kamlesh Sharma";
String splittedName[] = name.split(" ");
System.out.println(Arrays.toString(splittedName));
String firstName = splittedName[0];
String lastName = splittedName[1];
This will serve your purpose.
Note:
You need to provide delimiter in split method and not a name which you're passing

Related

How to include an argument while requesting for api in django?

I am trying to fatch live cricket score from sportsmonk cricket api and cricapi. using Django
everything goes fine until I request for any endpoint with a unique Id that is stored in a variable.
it gives key error always while doing so
my request :
resL = json.loads(requests.get(
'https://cricket.sportmonks.com/api/v2.0/fixtures/id?api_token').text)
here 'id' is a variable for a particular fixture.
It works fine while putting any numerical value instead of a variable.
Same is the case with this url :
resS = json.loads(requests.get(
'http://cricapi.com/api/fantasySummary/?apikey=123&unique_id=id).text)
I not getting what I am doing wrong here.
You need to indicate that the id param in your url is a variable. The way you have it written, it's just a string with the characters id.
One way to accomplish this is with f-strings:
id = 543 # or whatever your id is
api_token = 123
resL = json.loads(requests.get(
f'https://cricket.sportmonks.com/api/v2.0/fixtures/{id}?apikey=
{api_token}').text)
resS = json.loads(requests.get(
f'http://cricapi.com/api/fantasySummary/?apikey={api_token}&unique_id=
{id}).text)
When python parses these strings, it will replace {id} with the id you've defined as a variable elsewhere. Please note that you have to include the f at the beginning of the string.
Another way is with the older 'str'.format() method, which works as follows:
id = 543 # or whatever your id is
api_token = 123
resL = json.loads(requests.get(
'https://cricket.sportmonks.com/api/v2.0/fixtures/{}?apikey=
{}'.format(id, api_token)).text)
resS = json.loads(requests.get(
'http://cricapi.com/api/fantasySummary/?apikey={}&unique_id=
{}'.format(id, api_token)).text)
Here, the variables in the format() function will be substituted into the empty brackets in order. (you do not need the f here)

Newline not working with lists in embedded messages - Discord.py

I have a list with newlines as variable called result
['**Status: **Enabled\n**Q: **What is 2 + 2?\n **\\ A: **4\n\n**Author: **UltimateDucc#9121\n\n\n', '**Status: **Enabled\n**Q: **Where is Ethiopia?\n **\\ A: **Africa\n\n**Author: **BigSnacc#2466\n\n\n']
When I send it as an embedded message through discord:
l_msg = discord.Embed(
title = f'Page {list_msg}',
description = str(result),
colour = discord.Colour.blue()
)
await message.channel.send(embed = l_msg)
It comes out as this with every \n being ignored for whatever reason.
Any help is appreciated.
You need to convert the individual list entries into strings as opposed to just a string representation of the entire list. This is done with str.join().
Try changing the description line to:
description=''.join(result),
Result:

A quiz which requires a user to register and sign in

I was wondering if anyone could help me figure out how to allow a user to first register and then sign in to take a quiz. I’m finding it difficult as the users details such as name, age and year group as well as username and password need to be saved to a text file. I have some of the code for registration already completed but I'm still confused.
#quiz which tests a students knowledge
print "Welcome to my quiz!"
start = raw_input("Do you need to register or sign in?") #initial start up question
if start == "register": #registration
name = raw_input("What is your name?")
age = raw_input("What age are you?")
yearGroup = raw_input("What year of school are you in?")
username = (name[:3]+age)
print "You username is", username
password = raw_input("Please input a password which you will remember.")
text_file = open("user_info.txt", "w")
array = [name, age, yearGroup, username, password]
text_file.write(array)
text_file.close()
elif start == "sign in": #signing in
userEntry = raw_input("Please enter your username")
Your first issue is that you are writing the user data to the text file in the form of a list. when you try to read it from the file you will get a string that looks like "[name, age, yearGroup, username, password]". instead of turning it into a list, format it into a string with something like name+" "+str(age)+" "etc. then write it to the file. when you read the file you will get another string. Each user will be on a different line because you wrote them in sepperately so you can use .split("\n"). this will create an array where each element is a line in the file. Then you have to iterate through that and do .split() with no parameters (it automatically splits by any/all white space). If you apply all of this to get a list content where each element is the user data in the format [name, age, yearGroup, username, password] you will have:
f = open("user_info.txt", "r")
content = f.read()
content = content.split("\n")
for i in range(len(content)):
content[i] = content[i].split()
f.close()
if you want to turn the integer elements back to integers, then modify the for loop to do so:
for i in range(len(content)):
content[i] = content[i].split()
for j in range(len(content[i])):
try:
content[i][j] = int(content[i][j])
except:
pass

Get first name and last name out of a full name QString

I have a QT Project where I get the full name as a QString variable fullName from user input. I'd like to store the first name (firstName) and last name (surname) in their own QString variables by extracting them from fullName .
The user input could end up adding their middle names as well so I need to to work for all kind of examples like that, eg. user input: Barry Michael Doyle would have to give firstName the value of Barry and surname the value of Doyle.
I'm not very good at QString Manipulation as I've only recently started using the Qt Library. I'd appreciate all the help I could get.
Qt's documentation on QString is pretty thorough and has a lot of examples on how to use it. You can find it here:
http://doc.qt.io/qt-5/qstring.html
In general, parsing an input string that could have mistakes in it is difficult (what would the expected behavior of an input string of "BarryDoyle" be? What about "Barrydoy le"?)
For the simple case of splitting a string using the space character you can take a look at QString::split (http://doc.qt.io/qt-5/qstring.html#split)
QString str = "Barry Michael Boyle"
QStringList list_str = str.split(" ");
QString first = list_str.first();
QString last = list_str.back();

Highlighting the search phase IN sql

While it is easy to do replace of the the search text in the code after getting the dataset from an sql with a where clause:
x = x.Replace("SearchText","<span style='color:RED'>SearchText</span>");
I was wondering if there was a way to this IN the SQL.
Select x.Replace("SearchText","<span class='highlight'>SearchText</span>") as x from t where x like '%SearchText%'
or something like that.
The reason I am asking is because I do a:
COALESCE(LastName + ', ' + FirstName, LastName, FirstName) as Name
and I don't want a returned Name field of "Bobbly, Bob" to get two highlighted areas when searching for a LastName that includes "Bob" or a FirstName that includes "Bob" (Noting that first and last names have different search phrases"
Yeah, I could just return the two field separately and join them in code, but I just want to see if it can be done in SQL.
T-SQL includes a Replace Function
REPLACE ( string_expression , string_pattern , string_replacement )
That should do what you need
COALESCE(REPLACE(FirstName,'SearchText1','<span class=''highlight''>' + 'SearchText1' + '</span>')+','
+REPLACE(LastName,'SearchText2','<span class=''highlight''>'+'SearchText2'+'</span>')
,REPLACE(FirstName,'SearchText1','<span class=''highlight''>' + 'SearchText1' + '</span>')
,REPLACE(LastName,'SearchText2','<span class=''highlight''>'+'SearchText2'+'</span>')
,'')