首页 \ 问答 \ 拆分包含问号和等号的字符串的最佳方法(Best way to split a string containing question marks and equals)

拆分包含问号和等号的字符串的最佳方法(Best way to split a string containing question marks and equals)

有一个我有一个java字符串的问题:

String aString="name==p==?header=hello?aname=?????lname=lastname";

我需要在问号上分开,然后是等号。

结果应该是键/值对:

name = "=p=="
header = "hello"
aname = "????"
lname = "lastname"

问题是aname和lname成为:

name = ""
lname = "????lname=lastname"

我的代码只是通过执行aString.split("\\?",2)来拆分
它将返回2个字符串。一个包含键/值对,第二个字符串包含字符串的其余部分。 如果我在字符串中找到一个问号,我会对第二个字符串进行递归以进一步分解。

private String split(String aString)
 {
    System.out.println("Split: " + aString);
    String[] vals = aString.split("\\?",2);
    System.out.println("  - Found: " + vals.length);
    for ( int c = 0;c<vals.length;c++ )
       {
        System.out.println("  - "+ c + "| String: [" + vals[c] + "]" );
        if(vals[c].indexOf("?") > 0 )
          {
            split(vals[c]);
           }
        }
    return ""; // For now return nothing...
 }

任何想法我怎么可以允许一个名字?
免责声明:是的,我的正则表达式技能非常低,所以我不知道是否可以通过正则表达式来完成。


Having an issue where I have a java string:

String aString="name==p==?header=hello?aname=?????lname=lastname";

I need to split on question marks followed by equals.

The result should be key/value pairs:

name = "=p=="
header = "hello"
aname = "????"
lname = "lastname"

The problem is aname and lname become:

name = ""
lname = "????lname=lastname"

My code simply splits by doing aString.split("\\?",2)
which will return 2 strings.One contains a key/value pair and the second string contains the rest of the string. If I find a question mark in the string, I recurse on the second string to further break it down.

private String split(String aString)
 {
    System.out.println("Split: " + aString);
    String[] vals = aString.split("\\?",2);
    System.out.println("  - Found: " + vals.length);
    for ( int c = 0;c<vals.length;c++ )
       {
        System.out.println("  - "+ c + "| String: [" + vals[c] + "]" );
        if(vals[c].indexOf("?") > 0 )
          {
            split(vals[c]);
           }
        }
    return ""; // For now return nothing...
 }

Any ideas how I could allow a name of ?
Disclaimer: Yes , My Regex skills are very low, so I don't know if this could be done via a regex expression.


原文:https://stackoverflow.com/questions/23695951
更新时间:2022-04-15 07:04

最满意答案

它永远不会完美适合,因为count * (width + spacing)倍数count * (width + spacing)可能永远不会完全填充宽度,因为它们被舍入为整数值。

我现在使用的最佳近似值就是这样。 (还有一些其他代码可以从数据属性中读取设置。)

var $el = $(el);

var values = $el.data('values').split(',').map(parseFloat);
var type = $el.data('type') || 'line' ;
var height = $el.data('height') || 'auto';

var parentWidth = $el.parent().width();
var valueCount = values.length;
var barSpacing = 1;

var barWidth = Math.round((parentWidth - ( valueCount - 1 ) * barSpacing ) / valueCount);

$el.sparkline(values, {width:'100%', type: type, height: height, barWidth: barWidth, barSpacing: barSpacing});

剩下的唯一优化是使用barSpacing参数来减少圆角barWidth的乘积与父宽度之间的差异。


It will never fit perfectly because multiples of count * (width + spacing) may never fill the width exactly because they are rounded to integer values.

Best approximation I use now looks like that. (There is some additional code which reads the settings from data attributes.)

var $el = $(el);

var values = $el.data('values').split(',').map(parseFloat);
var type = $el.data('type') || 'line' ;
var height = $el.data('height') || 'auto';

var parentWidth = $el.parent().width();
var valueCount = values.length;
var barSpacing = 1;

var barWidth = Math.round((parentWidth - ( valueCount - 1 ) * barSpacing ) / valueCount);

$el.sparkline(values, {width:'100%', type: type, height: height, barWidth: barWidth, barSpacing: barSpacing});

The only optimisation left would be playing with the barSpacing parameter to reduce the difference between the product of the rounded barWidths and the parent width.

相关问答

更多
  • 我用Dan的解决方案解决了这个问题。 我拍摄了每个矢量的直方图(具有特定的bin间隔),并将它们全部存储在2D矩阵中(每列是一个完整的直方图)。 然后用image()显示它(无法访问imshow)。 我确实不得不乱用轴标签,因为image()函数是根据2D矩阵的坐标绘制它,而不是原始矢量中的值。 修复了一些调用set(gca,'YTickLabel / YTick')的问题。 还必须将YDir设置回“正常”而不是“反向”。 我认为image()正在翻转它。 I solved the problem usin ...
  • 我认为没有JasperReport服务器与任何其他图表库的集成是不可能的。在当前的Ireport中,您只能使用现有的堆栈条形图,然后您可以使用此属性设置图形属性“显示标签”,您可以在条形图中看到值。 I think it is not possible without integration of JasperReport server with any other chart libraries, In current Ireport you can use only existing stack bar ...
  • 调整输出.png更薄的一个! (例如500x300) 使用更宽的条! (例如50000) 使用xrange/yrange或 plot [:][:] 使用GP_VAL -s改进您的解决方案'自动',如下面的代码: set terminal pngcairo size 500, 300 set grid set tics font ", 10" set boxwidth 50000 set style fill solid set format y "" set ...
  • 它永远不会完美适合,因为count * (width + spacing)倍数count * (width + spacing)可能永远不会完全填充宽度,因为它们被舍入为整数值。 我现在使用的最佳近似值就是这样。 (还有一些其他代码可以从数据属性中读取设置。) var $el = $(el); var values = $el.data('values').split(',').map(parseFloat); var type = $el.data('type') || 'line' ; var hei ...
  • 成功! 这是双div#chart是“错误”。 当我将其更改为饼图的div#bchart和条形图的div#bchart ,条形图终于可见了! Success! It was the double div#chart that was the "error". When I changed it to div#pchart for the pie chart and div#bchart for the bar chart, the bar chart was finally visible!
  • 您可以在四分之一值之间添加一些空值,例如,您将获得所需的空间。 You could add some null values between the quarter values such as you will get the space you need.
  • 矩形定义为顶部x,顶部y,宽度和高度。 从第二个Rectangle参数中减去高度。 new Rectangle(i*(widthCalc/3), 150 - (int)height ...); Rectangles are defined as the top x, top y, width, and height. Subtract the height from your second Rectangle argument. new Rectangle(i*(widthCalc/3), 150 - ( ...
  • 这是一个传统的条形图...如果你横向思考。 您可以通过绘制具有正确宽度的
    元素,使用边框和填充颜色来创建HTML中的传统条形图。 您可以通过删除边框并使用包含线条图案的简单背景图像替换实心填充颜色,将其转换为您想要实现的条形。 It is a traditional bar graph... if you think laterally. You can create a traditional bar graph in HTML by drawing a
    element of the ...
  • % init x = randn(1000,1); y = randn(1000,1); nbins = [10 20]; % make histogram h = histogram2(x,y,nbins) % set limits and steps min_x = min(x); max_x = max(x); step_x = (max_x - min_x)/nbins(1); min_y = min(y); max_y = max(y); step_y = (max_y - min_y)/nb ...
  • 尝试使用列数作为因子,而不是列本身。 ggplot(data=example, aes(x=Length, y=Count, fill=factor(Count)))+geom_bar(stat="identity") 回报 ggplot会自动选择渐变色标,但是您可以使用不同的调色板或每个因素的手动颜色列表对其进行更改。 例如,您可以通过调用以下选项为数据集中的每个级别选择随机颜色: colors <- sample(colours(),length(levels(factor(example$Count ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)