• 5星
195KB m0_52957036 2020-10-09 23:57:14
• 5星
55KB m0_52957036 2020-03-02 21:58:14
• 5星
6KB thy77 2021-04-15 16:42:43
• 5星
441KB weixin_50804226 2021-07-15 22:02:47
• 5星
6.75MB weixin_43796045 2021-05-06 15:26:30
• 5星
1.11MB shooter7 2021-01-28 19:04:09
• 5星
28KB m0_52957036 2020-04-19 21:40:32
• 5星
1.77MB m0_52957036 2020-04-19 17:01:47
• 5星
142KB m0_52957036 2020-08-03 00:16:59
• I wrote a method that loops through a string and adds '/n' to create a line length that was given in the parameters. That description is not the best but it's hard to describe so look at the code belo...


I wrote a method that loops through a string and adds '/n' to create a line length that was given in the parameters. That description is not the best but it's hard to describe so look at the code below. Thanks in advance!
My Code:
public static String lineLength(String str, int length){
int totalLength = 0; //total length of the document
int lengthConst = 0; //constant value of the length for the loop
int nLength = 0; // length of \n = 2 characters
String work1, work2; //Strings to work with in the loop. Used as string buffers in substrings
if(str != null){
totalLength = str.length();
lengthConst = length;
}
if(length < 1){
throw new NullPointerException("Length must be >= 1");
}
/*
Main Loop: check again if length is not zero, check if totalLength is not zero,
check if pseudoCursor is not zero, check if length is less than or equal to totalLength
*/
while((length != 0) && (totalLength != 0) && (lengthConst != 0) && (length <= totalLength)){
work1 = str.substring(0, length); //store string of beginning to line length
work2 = str.substring(length + nLength, str.length()); //store string from length to end
work1 = work1.concat("\n"); //add new line
str = work1.concat(work2); //add work1 and work2 and store in str
nLength += 1; //nLength increases by 2 because we are going to add another \n
length += length;
}
return str;
}
When provided with the string "Daniel" and the new line length of 2 this is the run when printed to the console:
run:
Da
n
el
BUILD SUCCESSFUL (total time: 4 seconds)
解决方案
Here's a much simplified version
public static String lineLength(String str, int length) {
StringBuilder sb = new StringBuilder();
while(true) {
if(str.length() <= length) {
sb.append(str);
break;
}
sb.append(str.substring(0, length));
sb.append("\n");
str = str.substring(length);
}
return sb.toString();
}
You still need to understand what was wrong with your solution so that you learn from it and can apply that knowledge to the code you write in the future. Step through both this and your original code in a debugger and observe carefully what is happening.

展开全文
weixin_31582915 2021-07-16 20:01:43
• java字符串换行拼接字符串 java

@Test public void test(){ String s1="s1"+"\n"; String s2="s2"; System.out.println(s1+s2); }

@Test
public void test(){
String s1="s1"+"\n";
String s2="s2";
System.out.println(s1+s2);
}


展开全文
qq_43375661 2020-09-07 16:06:34
• 字符串:MIIC7jCCAlegAwIBAgIUfnKSsmMcfWy5GAoQXSvRUOCqDgAwDQYJKoZIhvcNAQEFBQAwWzEZMBcGA1UECh4QWSlaAYvaT+H/CG1Li9X/CTEbMBkGA1UECx4SWSlaAYvaT+H/CG1Li9X/CZDoMSEwHwYDVQQDHhhZK...原字符串:MIIC7...

原字符串:MIIC7jCCAlegAwIBAgIUfnKSsmMcfWy5GAoQXSvRUOCqDgAwDQYJKoZIhvcNAQEFBQAwWzEZMBcGA1UECh4QWSlaAYvaT+H/CG1Li9X/CTEbMBkGA1UECx4SWSlaAYvaT+H/CG1Li9X/CZDoMSEwHwYDVQQDHhhZK...
格式转变后的形式：
-----BEGIN CERTIFICATE-----
MIIC7jCCAlegAwIBAgIUfnKSsmMcfWy5GAoQ6uLPUFQWBwAwDQYJKoZIhvcNAQEF
BQAwWzEZMBcGA1UECh4QWSlaAYvaT+H/CG1Li9X/CTEbMBkGA1UECx4SWSlaAYva
T+H/CG1Li9X/CZDoMSEwHwYDVQQDHhhZKVoBi9pP4f8IbUuL1f8JdShiNwBDAEEw
HhcNMTIxMjE4MDAwMDAwWhcNMTMxMjE3MjM1OTU5WjBCMRMwEQYDVQQKDAppdHJ1
aUY0cAlSitbPAhV1ALRlM+XnbRGimnqcR1v32iiXEoPkybvLxmjdCN8uZ5eICCh9
xiZjLE/IJZWGksSL6PTKyQIDAQABo4HHMIHEMAkGA1UdEwQCMAAwCwYDVR0PBAQD
AgO4MBEGCWCGSAGG+EIBAQQEAwIGQDA0BgNVHSUELTArBggrBgEFBQcDAQYIKwYB
BQUHAwIGCisGAQQBgjcKAwMGCWCGSAGG+EIEATBhBgNVHR8EWjBYMFagVKBShlBo
dHRwOi8vMTkyLjE2OC4wLjEyNTo5MDgwL3B1YmxpYy9pdHJ1c2NybC5kbz9DQT0w
MUY3QzU4NEE4NkZBNjIyRjg4RjQ0OTc3MTg0ODIwMTANBgkqhkiG9w0BAQUFAAOB
gQBLY8VF4l9m9ZFGODyNfxrWkkH2ehnNSxzbXPZl0tGnfLE1Jgt1NLSwg98u8v4W
sLJeo/0Qd6R49ZPfS+bN6IGMOkWLeXs0WpRA+YyC8VnUgUDaqGfkVxKQ4vYuIi19
bUDuUQ7GPTO8QpZtf0tSocOi7FfGI8cWbm2LyXGnLl+FUQ==
-----END CERTIFICATE-----
求高手指定下........
展开

展开全文
weixin_39776344 2021-03-06 21:30:57
• // \n 换行符。 String test="1,测试啊啊。...也就是说，java字符串的 其他符号，换行符。回车符等，存入数据库，都是可以存进去的。 转载于:https://my.oschina.net/ouminzy/blog/835917

// \n 换行符。 String test="1,测试啊啊。\n "+ "2,对对对 "; System.out.println("=================>>>>>>>>>>>>>>>"+test); hrEffPfmcePlaneePo.setWorkContent(test); //存入数据库。 hrEffPfmcePlaneeDao.create(hrEffPfmcePlaneePo);

然后在前端 显示的时候，发现是 会把换行的效果也显示出来的。之前还担心存入数据库会不会不行。 也就是说，java 的字符串的 其他符号，换行符。回车符等，存入数据库，都是可以存进去的。

转载于:https://my.oschina.net/ouminzy/blog/835917
展开全文
chuncui2576 2019-09-12 07:19:55
• weixin_29323049 2021-03-04 06:12:12
• weixin_44125071 2020-12-16 08:26:09
• java实现页面字符串换行空格方法（如果原始字符串存在换行和空格） java 换行 空格

weixin_35954566 2021-02-12 14:47:17
• 29KB weixin_38620314 2020-09-05 13:50:26
• 63KB a369381959 2018-11-05 17:25:38
• java中后端字符串换行及格式，如何返回前台页面？ java 换行符

funnyfu0101 2021-09-15 19:34:00
• weixin_28956075 2021-03-15 23:23:43
• weixin_34037173 2011-09-18 20:10:52
• java 字符串中取消换行或添加换行 换行替换 java String 字符串

weixin_37705284 2019-03-21 19:45:16
• 9KB noreading 2014-07-15 17:46:37
• smile__________ 2020-04-16 15:19:36
• Intellij IDEA加号当字符串换行 idea换行字符数

weixin_42282699 2020-12-30 22:37:46
• weixin_43251783 2018-10-31 14:28:05
• java 拼接字符串在前端怎么实现换行 java

java_sha 2019-09-19 09:49:23
• a4228492 2019-09-25 03:25:46
• java基础（十七）：对字符串回车换行等格式类字符进行处理进行处理 java 正则表达式

qq_24607837 2020-09-23 16:56:18
• Java中的换行字符读取 - java java 读取换行

weixin_39574868 2021-03-21 10:44:45
• weixin_36484519 2021-02-27 15:39:10
• weixin_40003283 2020-12-02 15:40:53
• java转义字符之换行字符 Java \r 换行符

justry_deng 2018-06-25 11:13:00
• weixin_42245375 2020-04-18 17:10:31
• weixin_36190812 2021-03-14 15:40:09
• vue字符串换行 vue html js

javaniceyou 2020-07-09 10:02:23
• java后台 拼接字符串在前端textarea怎么实现换行 java html

weixin_44165280 2021-09-27 17:09:21

...

java 订阅