I need to convert a text string to an WinANSIEncoding compliant (Windows code page 1252) octal string (using Java).
"André" should become "101 156 144 162 351" or "\101\156\144\162\351".
I could use a simple string search and replace for each character in the list of allowed characters, but this would probably not be the fastest solutions.
Does anyone know how this should be done?
Thanx
TM
Ps
https://cryptii.com/text-octal
The toOctalString(int i) will help :
Using Streams
String str ="André";
String toOctal = str.chars().boxed().map(Integer::toOctalString)
.collect(Collectors.joining(" "));
System.out.println(toOctal); // 101 156 144 162 351
Workable Demo
Basic loop
String str ="André";
String toOctal ="";
for(char c : str.toCharArray()){
toOctal += Integer.toOctalString(c)+" ";
}
System.out.println(toOctal);
Related
I'm trying to convert Unix-style line-endings (LF) in a multi-line string to Windows-style (CR LF).
My plan of attack is to:
replace all CR LF instances with just LF
then replace all LF instances with CR LF
However, this snippet of code isn't matching the "\r\n":
String test = "test\r\ncase";
test.replaceAll("\r\n","\n");
PrintWriter testFile = new PrintWriter("test.txt");
testFile.print(test);
testFile.close();
I've already tried using double/triple/quadruple backslashes. No dice.
I also know that the test string doesn't contain a literal \r\n because it detects them as CR LF when printing to file.
What am I missing here?
You are not gettign the modified String from your code.
String are immutable so you need to save the returned value from replaceAll. There is no method that can change an instance of String
String test = "test\r\ncase";
//Print the character before
for(char c : test.toCharArray()){ System.out.print((int)c + " ");};
System.out.println();
//Save the replace result
test = test.replaceAll("\r\n","\n");
//Print the character after
for(char c : test.toCharArray()){ System.out.print((int)c + " ");};
Show that the test is first not changed then changed
116 101 115 116 13 10 99 97 115 101 //BEFORE
116 101 115 116 10 99 97 115 101 //AFTER
I have a string :
String str = "sces123 4096 May 27 16:22 sces123 abc";
I want to get sces123 abc from the string. My code is :
String[] line = str.split("\\s+");
String name = str.substring(str.indexOf(line[5]));
It returns the whole string.
Dont know how to do.
any help appreciated!
Your code should be
String[] line = str.split("\\s+");
String name = str.substring(str.lastIndexOf(line[5]));
because str.lastindexOf(line[5]) returns 0 and then the substring returns the whole String.
In your case you just need to change str.indexOf -> str.lastIndexOf.
This is one easy solution :
String str = "sces123 4096 May 27 16:22 sces123 abc";
//split spaces
String[] line = str.split(" ");
//get 2 last columns
String name = (line[5] + " " + line[6]);
System.out.println(name);
As Glorfindel said in the comment sces123 which is the content of if line[5] also contain as the first substring in the main String str. That why you are getting the full string.
Whats really happening here is:
indexOf( line[ 5 ]) --> returning 0
str.substring(0) --> returning substring form 0 to last which is the main string
If you are only doing the hard codded things then i don't see the purpose of you here.
But What you want you get in this way (if it serve your purpose ) :
String name = str.substring( str.indexOf( line[ 5 ]+" "+line[6] ) );
Try This:
String str = "sces123 4096 May 27 16:22 sces123 abc";
String[] line = str.split("\\s+");
System.out.println(str.substring(str.lastIndexOf(line[5])));
You could use a Matcher to find the end of the 5th match:
String str = "sces123 4096 May 27 16:22 sces123 abc";
Pattern p = Pattern.compile("\\s+");
Matcher m = p.matcher(str);
for (int i = 0; i < 5; i++) {
m.find();
}
String name = str.substring(m.end());
In my opinion this is better than using lastIndexOf on to concatenating elements at indices 5 and 6, for the following reasons:
It does not require line[5] to be the last occurence of that string.
Using lastIndexOf doesn't work for input
"sces123 4096 May 27 16:22 sces123 sces123"
It also works for seperator strings of arbirtrary length.
Using line[ 5 ]+" "+line[6] doesn't work for input
"sces123 4096 May 27 16:22 sces123 abc"
It does not require the number elements after the split to be 7.
Using line[ 5 ]+" "+line[6] doesn't work for input
"sces123 4096 May 27 16:22 sces123 abc def"
I have file with data format as
userid, friend id, books id, cd id
1, 11 12 14 12, 223 256 333 234 222, 22
2, 78 22, 22 66 11 29, 76 455
3, 123 22 11 234 198 122 881, 34 12 98 64, 22
where I need to use only user id and cd id, But I am unable to separate these fields.
My Java code as below.
BufferedReader in = new BufferedReader(new FileReader("CSV_test.txt"));
BufferedWriter ou =new BufferedWriter(new FileWriter("users.csv"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
String[] ar = str.split(",");
String[] ar1 = ar[1].split("");
ou.write(ar[0]);
ou.write(",");
ou.write(ar1[1]);
ou.newLine(); }
in.close();
ou.close();
}
Is there any issue with this?
Surely you want
String[] ar = str.split(",");
String user = ar[0].trim();
String cd = ar[3].trim();
Note that I'm trimming to remove leading/trailing spaces.
You could split using ", " (note the trailing space) and that would remove the need to further trim(). It does make some assumptions however as to how your fields are separated (commas ? commas-and-spaces?) and perhaps it's worth investigating a CSV library.
No need to reinvent the wheel. While CSV parsing is fairly simple, there are things that might be a little bit complicated (such as escaping the separator in field values). Existing libraries can do this for you, such as OpenCSV or CsvJdbc
Hii Guys !!!
I have a string with values like 69 17 17 16 2 1 1 26 26 56 56 69 20 19 20 etc .Now As per my need i have to put these values into new String with each values in new line as after each value space is there ..
Any help will be highly appreciated..
Thanx in advance...
String newStr = origStr.replaceAll(" ", " \n");
You should split the String using a specific separator into a List.
Then print out the List using the format required.
This helps when tomorow the String contains digits, decimals, text, etc or they want the text in another format.
String source = "69 17 17 16 2 1 1 26 26 56 56 69 20 19 20";
String[] splitted = source.split(" ");
StringBuilder sb = new StringBuilder();
for (String split : splitted){
sb.append(split).append(System.getProperty("line.separator"));
}
System.out.println(sb.toString());
Hi I was recently developing a code where i had to extract the last 3 group of digits. So i used pattern to extract the data. But i failed to understand. CAN any one help me to understand it ??
String str ="EGLA 0F 020";
String def = "ALT 1F 001 TO ALT 1F 029";
String arr[] = def.split("TO");
String str2 = arr[0];
System.out.println("str2:"+str2);
Pattern pt = Pattern.compile("[0-9][0-9][0-9]$");
Matcher m1 = pt.matcher(str);
Matcher m2 = pt.matcher(str2);
boolean flag = m1.find();
boolean flag2 = m2.find();
if(flag)
System.out.println("first match:::"+m1.group(0));
else
System.out.println("Not found");
if(flag2)
System.out.println("first match:::"+m2.group(0));
else
System.out.println("Not found");
The output produced for the above code is As follows:::
str2:ALT 1F 001
first match:::020
Not found
Please Do reply iam stuck here ??
It's because when you split you have a trailing space.
String str = "EGLA 0F 020";
String str2 = "ALT 1F 001 ";
// ^ trailing space
You could fix it a number of ways. For example:
by splitting on " TO "
trimming the result
allowing trailing spaces in your regular expression.
For example, this change would work:
String arr[] = def.split(" TO ");
If you notice your split take effect only on the letters "TO", it means str2 pattern is "ALT 1F 001 ".
To resolve this you can try to split on "\s*TO\s*" instead of "TO" so that any spaces surrounding the work TO would be removed too. Another solution would be to replace your pattern "[0-9][0-9][0-9]$" with "[0-9][0-9][0-9]" without the final $, so that it would accept ending spaces on your String.
Try this pattern:
Pattern pattern = Pattern.compile("[0-9][0-9][0-9]\\s*$");
or
Pattern pattern = Pattern.compile("[0-9]{3}\\s*$");