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 2 years ago.
Improve this question
import string
def main():
inString =input("enter any number to decode : ")
message=" "
for numStr in string.split(instring):
asciiNum = eval(numStr)
message = message+chr(asciiNum)
print("\n", message)
main()
You need to convert the input to int by using int() function because the input is a string and ASCII is a int
To get the ASCII value, you can use chr()
So, for example:
inString = input("enter any number to decode : ")
number_to_decode = int(inString)
print( chr( number_to_decode ) )
It should work:
def main():
instring = input("enter any number to decode : ")
message = " "
for numStr in str.split(instring):
asciiNum = eval(numStr)
message = message + chr(asciiNum)
print("\n", message)
Here is a Java soln,
class AsciiConvertor{
public static void main(String[] args) {
int asciiVal = 87;
String strValue = Character.toString((char) asciiVal);
System.out.println(strValue);
}
}
Output: W
Related
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 2 years ago.
Improve this question
how can I say that is my String match with this pattern: (install OS #name #version) which name and version can be any String without white space.
As you can use following Code:
// assuming parenthesis an sharp are pattern included:
String s = "(install OS #testname #testversion)";
if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
String[] splitted = s.split("\\s");
String name = splitted[2].replace("#", "");
String version = splitted[3].replace(")", "").replace("#", "");
System.out.println("name: " + name);
System.out.println("version: " + version);
}
// assuming parenthesis an sharp are both not pattern included:
s = "install OS testname2 testversion2";
if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
String[] splitted = s.split("\\s");
String name = splitted[2];
String version = splitted[3];
System.out.println("name: " + name);
System.out.println("version: " + version);
}
(if you need to allow mutliple whitespaces between parts, you have replace in both, split regex and match regex, \s with \s+)
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 3 years ago.
Improve this question
I have a path /departments/{dept}/employees/{id}. How do I obtain dept and id from path /departments/{dept}/employees/{id}?
For example, I would like to obtain dept1 and id1 if path is /departments/dept1/employees/id1
I tried
String pattern1 = "departments/"
String pattern2 = "/employees"
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
Matcher m = p.matcher(text);
while (m.find()) {
String a = m.group(1);
}
Is there a simpler way to obtain dept1 and id1? I would prefer not using string.split as I have different paths for which I want to obtain path parameters and I prefer not having a dependency on the index position of the path parameters.
Using Spring... or:
String url = /departments/{dept}/employees/{id}
/----none--/-dept-/---none---/-id-
Make a split of url and get the position of array 1 and 3:
String urlSplited = url.split("/");
String dept = urlSplited[1];
String id = urlSplited[3];
If you are using Spring framework, then you can use a class specifically for this purpose named AntPathMatcher and use its method extractUriTemplateVariables
So, you can have the following:
AntPathMatcher matcher = new AntPathMatcher();
String url = "/departments/dept1/employees/id1";
String pattern = "/departments/{dept}/employees/{id}";
System.out.println(matcher.match(pattern, url));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("dept"));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("id"));
If you prefer regix:
import org.junit.Test;
import java.util.regex.Pattern;
public class ExampleUnitTest {
#Test
public void test_method() throws Exception {
Pattern digital_group = Pattern.compile("[//]");
CharSequence line = "test/message/one/thing";
String[] re = digital_group.split(line);
for (int i=0;i<re.length;i++) {
System.out.println(re[i]);
}
}
} //END: class ExampleUnitTest
The output is:
test
message
one
thing
Process finished with exit code 0
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
I have a reply message in a String and I want to split it to extract a value.
My String reply message format is:
REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...);
My requirement is to get the value ABC024049364194 from the above string format.
I tried using this code, but it hasn't worked:
String[] arrOfStr = str.split(",", 5);
for (String a : arrOfStr)
System.out.println(a);
If you split the String, you will simply need to access the token at index 2.
// <TYPE>(<ARGUMENTS>)
String message = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE);";
Matcher m = Pattern.compile("(\\w+)\\((.+)\\)").matcher(message);
if (m.find()) {
String type = m.group(1);
String[] arguments = m.group(2).split(",");
System.out.println(type + " = " + arguments[2]); // REPLY = ABC024049364194
}
public static void main(String[] args) {
String str = "REPLY(MSGID,15,ABC024049364194,SERVICE,10,CREATE,...)";
String code = str.split(",")[2];
System.out.println(code);
}
Will work if your code is always after 2 coma
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 8 years ago.
Improve this question
Here is my code:
public String display() {
return "\n......................\nFixed Employee:\n" +
"Name: " + super.fullName() +
"\nSalary: " + salary() +
" tk\n......................";
}
But when I'm invoking this method from main class, "\n" newLine not working. just showing one line output. Will you plz help to solve the problem?
Thanks
For saving in files use \r\n. \n as new lines is viable on printstreams but not writing to files.
You may need the system independent line separator as it might differ from one OS to another. Just replace the \n with the value of line separator:
I can be retrieve as you load any system property:
public String display() {
String separator = System.getProperty("line.separator"); // Load the system property using its key.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}
Or simply use System#lineSeparator method as #Deepanshu Bedi suggested:
public String display() {
String separator = System.lineSeparator(); // Consider it as a shortcut.
return "\n......................\nFixed Employee:\n"
+ "Name: "
+ super.fullName() +
"\nSalary: "
+ salary()
+ " tk\n......................"
.replace("\\n", separator); // replace the \n before returning your String
}
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
my string is in this format:
(notice all elements are being delimited by a space)
data = "row_id email year month day path"
so the string should look like this:
data ="3 email#goofy.com 2013 July 13 c:\hold_files\lisa.jpg"
I need to get each element in that string in following variables:
String sRow_id, sEmail, sYear sDay sPath;
does anyone have any ideas on how to do this ?
You can split your string using split() method of String class, which gives you an array of String .
Something like this :
String[] splitArray = data.split("\\s+"); // for more than one spaces
String[] splitArray = data.split("\\s"); // exactly one space
i need these elements in variables, not array.
You can assign each array element to one of your variables , using simple array index.
Use .split() to split the String by the space delimiter
Like so:
data ="3 email#goofy.com 2013 July 13 c:\\hold_files\\lisa.jpg";
String[] split = data.split("\\s");
String sRow_id = split[0];
String sEmail = split[1];
String sYear = split[2];
String sDay = split[3];
String sPath = split[4];
Log.i(TAG, sRow_id + " " + sEmail + " " + sYear + " " + sDay + " " + sPath);
Use a Scanner:
String data ="3 email#goofy.com 2013 July 13 c:\hold_files\lisa.jpg";
Scanner scannerInstance = new Scanner(data);
sRowID = scannerInstance.next();
sEmail = scannerInstance.next();
sYear = scannerInstance.next();
sDay = scannerInstance.next();
sPath = scannerInstance.next();
Using split is another way to do this, as is StringTokenizer.